I'm relatively new to programming, and am currently learning C++. I'm doubtful if my logic in this is even correct, but here's something I've been trying to work out:
I have a simple program that outputs the size and content of a vector;
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{ 10 };
vector<int> v5{ 10, 42 };
vector<string> v6{ 10 };
vector<string> v7{ 10, "hi" };
//and so on...........
bool firstPass= true;
for (auto i : v3){
if (firstPass) cout << "Vector size: " << v3.size() << ".\nElements: " << i << ", ";
cout << i << ", ";
firstPass= false;
}
If i want to iterate through another vector, i have to manually change the v3 to vX, but what i'd like is for this snippet of code to go through all the vectors.
I've tried several methods, such as creating a
vector<string> V8{"v1","v2","v3"..}
for (auto i : V8[counter])
but essentially I've failed because "v1" != v1.(This is where i got the "convert string to vector" idea from, but something tells me this isn't the way to go about doing this...)
Any help & criticism would be greatly appreciated, and i apologize as this will probably get filed under too specific or even useless, considering my way of trying to solve this is probably faulty and i asked the wrong question!
vector<vector<int>> = { { }, {10}, {10,42}, {10}, {10,42} };. You can then iterate using nestedforloops. But you can't mix in strings so easily - you could have a separatevector<vector<string>>, or you could create an abstraction layer using variants (boost::any,boost::variant, an OO runtime-polymorphic hierarchy for the vectors)... it's probably beyond what you're ready to have to write.vector<vector<int>>, without having tovectorOfVectors.push_back(vectorName)for each vector? The following code would write v1,v2,v3 in place ofvectorNameeach time, but those would be strings and hence of the wrong type:vectorOfVectors.push_back("v"+to.string(counter)) counter++;