2013-10-20 30 views
0

我有几个向量被保存在一个向量中。我必须对它们执行某些逻辑操作,如果操作成功完成,我必须存储保存在arrayOfUsers中的该向量。问题是我无法访问arrayOfusers中存储的特定向量直接访问向量中的向量阵列

示例:arrayOfUsers有3个向量存储在其中,它通过了逻辑操作,我必须将向量编号2写入文件中。我不能索引中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); 

我的for循环

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 

      } 
    } 
+1

因为它的内部为for循环 – meWantToLearn

+0

当'logicOperationPassed'变为'true'保存值'y'在另一个变量的内部for循环 – P0W

回答

0

运行直接访问矢量为什么你认为的“Y”矢量指向?看起来它应该指向一个字符串。

x是“arrayOfUsers”中的一个元素,y是其中一个元素。

FWIW - 你似乎正在使用一些C++ 11功能(自动),而不是所有的方式。为什么不一样的东西:

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. 
} 

的命名会为你处理与各级什么清晰,类型是微不足道的,从汽车所以没有大的涨幅。

0
#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; 
    } 


}