C++ 现有的恶言向量

C++ 现有的恶言向量,c++,c++11,object,vector,C++,C++11,Object,Vector,我有一些对象(在本例中它们是向量),我希望它们存储在向量中,但不知道如何正确声明它。 我的代码是: vector<string> A; vector<string> B; vector<vector<string>> Tables={A,B}; 然后用simpleA[i]阅读表格,然后在更复杂的函数中使用。您必须在表格中存储vector&或vector*。 后者应该有效,但我试图管理第一个。 关于这一点,我发现。 因此,我尝试了其他方法:std:

我有一些对象(在本例中它们是向量),我希望它们存储在向量中,但不知道如何正确声明它。 我的代码是:

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

然后用simple
A[i]
阅读表格,然后在更复杂的函数中使用。

您必须在表格中存储vector&或vector*。 后者应该有效,但我试图管理第一个。 关于这一点,我发现。 因此,我尝试了其他方法:std::reference\u包装器 这似乎是一个解决方案:

#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;
}
使用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 . . .

顺便说一句:如果我们在源代码中嵌入数据表,我们更喜欢普通的旧数据(例如常量字符的C数组)。因此,编译器可以在编译时完全布局存储。可能不会出现任何问题(由于施工顺序等原因)。您的示例是可行的,但您能否编写一个示例,说明如何使用表格附加“A 3”?Simply tables[0]。push_back(“A 3”)在编译时返回以下错误:“push_back”:不是具有[_Ty=std::string]@Math的“std::reference_包装器”的成员Guy:我编辑了我的示例。请注意,我更喜欢较新的vector::emplace_back(),它具有“就地”构造。@Math Guy:现在,我意识到了您问题中强调的文本,并再次修改了示例:a[1]通过表访问它被修改。@Math Guy:我只是改进了示例,以更清楚地显示a和表[0]确实共享了相同的存储。当我上传时,我意识到你已经接受了我的答案。。。
$ 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'
&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 . . .