I have several vectors that are being saved in a vector. I have to do certain logic operations on them, if the operations are successfully done, i have to store that vector which is saved in arrayOfUsers. issue is I cannot access the specific vector stored inside the arrayOfusers
example: arrayOfUsers has 3 vectors stored inside it, afer it passes the logic operations i have to write the vector number 2 in a file. I cannot access directly the vector by index inside arrayOfUsers
vector<string> usersA ("smith","peter");
vector<string> usersB ("bill","jack");
vector<string> usersC ("emma","ashley");
vector<vector<string>> arrayOfUsers;
arrayOfUsers.push_back(usersA);
arrayOfUsers.push_back(usersB);
arrayOfUsers.push_back(usersC);
I run for loop
for ( auto x=arrayOfUsers.begin(); x!=arrayOfUsers.end(); ++x)
{
for (auto y=x->begin(); y!=x->end(); ++y)
{
//logic operations
}
if(logicOperationPassed== true)
{
// i cannot access the vector here, which is being pointed by y
//write to file the vector which passed its logic operation
// i cannot access x which is pointed to the arrayOfUsers
// ASSUMING that the operations have just passed on vector index 2,
//I cannot access it here so to write it on a file, if i dont write
//it here, it will perform the operations on vector 3
}
}
Why do you think "y" is pointing at a vector? Looks like it should be pointing at a string.
x is one element from "arrayOfUsers", y is one element from one of those.
FWIW - you seem to be using some c++11 features (auto) while not going all the way. Why not something like:
string saved_user;
for (const vector<string>& users : arrayOfUsers) {
for (const string& user : users) {
...
... Logic Operations and maybe saved_user = user ...
}
// If you need access to user outside of that loop, use saved_user to get to
// it. If you need to modify it in place, make saved_user a string* and do
// saved_user = &user. You'll also need to drop the consts.
}
The naming will be clearer as to what you're dealing with at each level, and the types are trivial, so no major gains from auto.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> usersA;
vector<string> usersB;
vector<string> usersC;
usersA.push_back("Michael");
usersA.push_back("Jackson");
usersB.push_back("John");
usersB.push_back("Lenon");
usersC.push_back("Celine");
usersC.push_back("Dion");
vector <vector <string > > v;
v.push_back(usersA);
v.push_back(usersB);
v.push_back(usersC);
for (vector <vector <string > >::iterator it = v.begin(); it != v.end(); ++it) {
vector<string> v = *it;
for (vector<string>::iterator it2 = v.begin(); it2 != v.end(); ++it2) {
cout << *it2 << " ";
}
cout << endl;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.