i would like to iterate through a vector and check if elements are vectors or strings. Also i need a way to pass different vecors to a function.
Something like this:
using namespace std;
string toCustomString(<some vector> vec) {
string ret = "";
for(size_t i = 0; i < vec.length(); ++i)
if (vec[i] == %vector%)
ret += toCustomString(vec[i]);
else //if type of vec[i] is string
ret += "foo"+vec[i]+"bar";
}
return ret;
}
-
Well, first i need to know how i can check correctly if vec[i] is a std::vector
-
Then i need to know how to define the paramater for the function to accept any kind of (multidimensional) vector
std::vectorcan only contain one type – that is theTinstd::vector<T>, which can be accessed with the membervalue_type.What you probably are looking for is template specialization:
(if you want to partially specialize it over all vectors then you’ll need to lift it to a struct)
If you really want to store both strings and vectors in the vector then look at Boost.Variant and Boost.Any