I have several collections of different types of objects and various types of containers in C++. I need to perform some common operations in this collections, for instance, clean and resize the containers. Is there a way to do it writing a short code? For example, consider the following code:
#include <vector>
#include <unordered_map>
using namespace std;
class A {};
class B {};
class C {};
int main() {
vector<A> a;
vector<B> b;
vector<C> c;
unordered_map<int, int> map;
// I would like something like this...
for(auto& ct : {a, b, c, map}) {
ct.clear();
ct.reserve(1000);
}
return 0;
}
Of course, the code above is invalid on C++11, since the compiler cannot deduce the list. I have more or less ten containers but these number can go up. So, the code must be equivalent (in size) to apply the operations to these containers separately.
Thanks very much for your help.