【问题标题】:vector of existing objests现有对象的向量
【发布时间】:2017-02-04 10:12:10
【问题描述】:

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

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

当然,在将其复制到 Tables 之前,它会复制 AB。 问题是:如何创建指向AB 的指针表,以便在使用Tables 时实际操作它们?

我希望能够写信给AB 使用

Tables[position].push_back(sub);

并使用简单的A[i] 读取表格,然后在更复杂的功能中使用它。

【问题讨论】:

    标签: c++ c++11 object vector


    【解决方案1】:

    您必须在表格中存储 vector& 或 vector*。 后者应该可以工作,但我试图管理第一个。 关于这个我发现SO: Why can't I make a vector of references?。 因此,我尝试了其他方法:std::reference_wrapper 它似乎是一个 (/the) 解决方案:

    #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'
    

    使用 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 . . .
    

    【讨论】:

    • 顺便说一句:如果我们在源代码中嵌入数据表,我们更喜欢普通的旧数据(例如 const char 的 C 数组)。因此,编译器可以在编译时完全布局存储。不会出现任何问题(由于施工顺序等)。
    • 您的示例有效,但您可以编写一个示例如何使用表格附加“A 3”吗?简单的 tables[0].push_back("A 3") 在编译时返回以下错误:'push_back': is not a member of 'std::reference_wrapper<:vector>>>' 与 [ _Ty=std::string ]
    • @Math Guy:我编辑了我的样本。请注意,我更喜欢更新的 vector::emplace_back() ,它执行“就地”构造。
    • @Math Guy:现在,我意识到您问题中强调的文本并再次修改了示例:修改了 a[1] 通过表格访问它。
    • @Math Guy:我刚刚改进了示例,以更清楚地表明 a 和 tables[0] 确实共享相同的存储。当我上传时,我意识到你已经接受了我的回答......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 2021-03-31
    • 2011-10-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    相关资源
    最近更新 更多