Hello, I would like a C++ constructor/method to be able to take any container as argument. In C# this would be easy by using IEnumerable, is there an equivalent in C++/STL ?
Anthony
Hello, I would like a C++ constructor/method to be able to take any container as argument. In C# this would be easy by using IEnumerable, is there an equivalent in C++/STL ?
Anthony
The C++ way to do this is with iterators. Just like all the <algorithm>
functions that take (it begin, it end, )
as first two parameters.
template <class IT>
T foo(IT first, IT last)
{
return std::accumulate(first, last, T());
}
If you really want to go passing the container itself to the function, you have to use 'template template' parameters. This is due to the fact that C++ standard library containers are not only templated with the type of the contained type, but also with an allocator type, that have a default value and is therefore implicit and not known.
#include <vector>
#include <list>
#include <numeric>
#include <iostream>
template <class T, class A, template <class T, class A> class CONT>
T foo(CONT<T, A> &cont)
{
return std::accumulate(cont.begin(), cont.end(), T());
}
int main()
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
std::list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
std::cout << foo(v) << " " << foo(l) << "\n";
return 0;
}