2017-02-04 8 views
0

私はいくつかのオブジェクト(この例ではベクトルです)をベクトルに格納したいが、正しく宣言する方法はわかりません。 私のコードは次のとおりです。もちろん既存objestsのベクトル

vector<string> A; 
vector<string> B; 
vector<vector<string>> Tables={A,B}; 

それはTablesにそれをコピーする前ABを複製します。 質問:ABにポイターのテーブルを作成するにはどうすればいいですか?実際にはTablesを使用していますか?

私は

Tables[position].push_back(sub); 

を使用してABに書き込み、簡単なA[i]でテーブルを読み、後でより複雑な機能でそれを使用することができるようにしたいです。

答えて

2

ベクトル&またはベクトル*をテーブルに保存する必要があります。 後者はうまくいくはずですが、最初の管理を試みました。 これについてはSO: Why can't I make a vector of references?が見つかりました。

#include <functional> 
#include <iostream> 
#include <string> 
#include <vector> 

using namespace std; 

int main(int, char**) 
{ 
    // build contents of tables 
    vector<string> a { string{"A 0"}, string{"A 1"}, string{"A 2"} }; 
    cout << "&a: " << hex << (size_t)&a << endl; 
    vector<string> b { string{"B 0"}, string{"B 1"}, string{"B 2"} }; 
    cout << "&b: " << hex << (size_t)&b << endl; 
    vector<std::reference_wrapper<vector<string> > > tables { 
    a, b 
    }; 
    // print contents of tables 
    for (size_t i = 0, n = tables.size(); i < n; ++i) { 
    const vector<string> &tbl = tables[i]; 
    cout << "&tables[" << i << "]: " << hex << (size_t)&tbl << endl; 
    for (size_t j = 0, m = tbl.size(); j < m; ++j) { 
     cout << "tables[" << i << "][" << j << "]: '" 
     << tbl[j] << "'" << endl; 
    } 
    } 
    // append another string to a 
    { vector<string> &tbl = tables[0]; 
    tbl[1] = string("A 1 modified"); 
    tbl.push_back(string("A 3")); 
    tbl.emplace_back(string("A 4")); 
    } 
    // print contents of a 
    for (size_t i = 0, n = a.size(); i < n; ++i) { 
    cout << "a[" << i << "]: '" << a[i] << "'" << endl; 
    } 
    // append another table 
    vector<string> c { string("C 0"), string("C 1"), string("C 2") }; 
    cout << "&c: " << hex << (size_t)&c << endl; 
    /* my 1st guess: 
    tables.emplace_back(std::ref(c)); 
    * but even this works: 
    */ 
    tables.emplace_back(c); 
    // print contents of tables 
    for (size_t i = 0, n = tables.size(); i < n; ++i) { 
    const vector<string> &tbl = tables[i]; 
    cout << "&tables[" << i << "]: " << hex << (size_t)&tbl << endl; 
    for (size_t j = 0, m = tbl.size(); j < m; ++j) { 
     cout << "tables[" << i << "][" << j << "]: '" 
     << tbl[j] << "'" << endl; 
    } 
    } 
    // done 
    return 0; 
} 

はcygwinの上でgccでこれをコンパイル:

$ gcc --version 
gcc (GCC) 5.4.0 
Copyright (C) 2015 Free Software Foundation, Inc. 

$ g++ -std=c++11 -o testVecRef testVecRef.cc 

$ ./testVecRef 
&a: 61cbec 
&b: 61cbe0 
&tables[0]: 61cbec 
tables[0][0]: 'A 0' 
tables[0][1]: 'A 1' 
tables[0][2]: 'A 2' 
&tables[1]: 61cbe0 
tables[1][0]: 'B 0' 
tables[1][1]: 'B 1' 
tables[1][2]: 'B 2' 
a[0]: 'A 0' 
a[1]: 'A 1 modified' 
a[2]: 'A 2' 
a[3]: 'A 3' 
a[4]: 'A 4' 
&c: 61cbc8 
&tables[0]: 61cbec 
tables[0][0]: 'A 0' 
tables[0][1]: 'A 1 modified' 
tables[0][2]: 'A 2' 
tables[0][3]: 'A 3' 
tables[0][4]: 'A 4' 
&tables[1]: 61cbe0 
tables[1][0]: 'B 0' 
tables[1][1]: 'B 1' 
tables[1][2]: 'B 2' 
&tables[2]: 61cbc8 
tables[2][0]: 'C 0' 
tables[2][1]: 'C 1' 
tables[2][2]: 'C 2' 

は、コンパイルとで開始(/)ソリューションを reference_wrapperのstd ::、あるように思わ: はこのように、私は何か他のものを試してみましたMS VS2013:

&a: cfba2ff2b8 
&b: cfba2ff378 
&tables[0]: cfba2ff2b8 
tables[0][0]: 'A 0' 
tables[0][1]: 'A 1' 
tables[0][2]: 'A 2' 
&tables[1]: cfba2ff378 
tables[1][0]: 'B 0' 
tables[1][1]: 'B 1' 
tables[1][2]: 'B 2' 
a[0]: 'A 0' 
a[1]: 'A 1 modified' 
a[2]: 'A 2' 
a[3]: 'A 3' 
a[4]: 'A 4' 
&c: cfba2ff508 
&tables[0]: cfba2ff2b8 
tables[0][0]: 'A 0' 
tables[0][1]: 'A 1 modified' 
tables[0][2]: 'A 2' 
tables[0][3]: 'A 3' 
tables[0][4]: 'A 4' 
&tables[1]: cfba2ff378 
tables[1][0]: 'B 0' 
tables[1][1]: 'B 1' 
tables[1][2]: 'B 2' 
&tables[2]: cfba2ff508 
tables[2][0]: 'C 0' 
tables[2][1]: 'C 1' 
tables[2][2]: 'C 2' 
Drücken Sie eine beliebige Taste . . . 
+0

Btw:データテーブルをソースコードに埋め込むと、普通の古いデータ(たとえばconst charのC配列)が優先されます。したがって、コンパイラはコンパイル時に完全にストレージをレイアウトできます。 (建設工事などにより)問題が発生することはありません。 – Scheff

+0

あなたの例は動作しますが、テーブルを使って "A 3"を追加する方法の例を書くことはできますか? 'push_back': 'std :: reference_wrapper 'のメンバではありません。単純にテーブル[0] .push_back( "A 3")は次のエラーを返します。 >> ' と [ _Ty = std :: string ] –

+0

@Math Guy:サンプルを編集しました。私はより新しいベクトル<> :: emplace_back()を好むことに注意してください。これは "インプレース"構築を行います。 – Scheff