This concept is known as generic programming. In C++, you use templates to achieve this, and specifically a function template. If you want different types of lists with different types to be auto-deduced, or have a specific need for advanced templates, you can also use a template-template.
An example:
#include <iostream>
#include <vector>
#include <list>
template < template < class, class > class V, class T, class A >
void erase_value(V<T, A>& v, const T& t)
{
typename V<T,A>::iterator s = v.begin();
while (s != v.end()) {
if ((*s) == t) { v.erase(s); break; }
++s;
}
}
template < typename T >
void print_all(T begin, T end)
{
for (; begin != end; ++begin) {
std::cout << *begin << " ";
}
std::cout << std::endl;
}
template < typename T >
void print_all(const T& array)
{
for (auto i : array) {
std::cout << i << " ";
}
std::cout << std::endl;
}
int main(int argc, char** argv)
{
std::vector<std::string> strings {"123","321","ABC"};
std::list<int> ints {123,321,5332};
print_all(strings);
print_all(ints);
erase_value(strings, std::string("123"));
erase_value(ints, 123);
print_all(strings.begin(), strings.end());
print_all(ints.begin(), ints.end());
return 0;
}
Hope that can help.