Obviously, standard containers do not have a common base class or common interface, although the method names are uniform.
Problem: I have to fill the container with a collection of objects of a unique type. The container may be std::list, a std::vectoror, std::dequeand possibly some other custom container. Is the following code the best solution?
# include <string>
# include <iostream>
# include <list>
# include <vector>
# include <deque>
template<typename T>
void f(T & t)
{
t.clear() ;
t.push_back("Alice") ;
t.push_back("Bob") ;
}
int main(int, char*[])
{
std::list<std::string> l ;
std::vector<std::string> v ;
std::deque<std::string> q ;
f(l) ;
f(v) ;
f(q) ;
return 0 ;
}
Thanks for any advice!
EDIT
Here is an example that I illustrated with help fin my first post:
struct AudioFormat
{
uint32_t samplerate ;
uint8_t channels ;
uint8_t bitdepth ;
} ;
class AudioDevice
{
public :
void GetSupportedAudioFormats(std::list<AudioFormat> &) ;
} ;
I am looking for a better way to declare GetSupportedFormatsso that it can handle many other containers, not just std::lists. This is the point of my first post.