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 ( 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

           }
    }
4

2 に答える 2

0

「y」がベクトルを指しているのはなぜだと思いますか? 文字列を指しているように見えます。

x は「arrayOfUsers」の 1 つの要素であり、y はそれらのいずれかの 1 つの要素です。

FWIW - 完全に機能していないのに、いくつかの c++11 機能 (auto) を使用しているようです。次のようなものではないのはなぜですか:

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

ネーミングは、各レベルで何を扱っているかについてより明確になり、タイプは自明であるため、auto からの大きな利点はありません。

于 2013-10-20T20:28:54.337 に答える
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;
    }


}
于 2013-10-20T21:12:48.297 に答える