gpt4 book ai didi

c++ - 现有对象的 vector

转载 作者:行者123 更新时间:2023-11-30 03:34:24 25 4
gpt4 key购买 nike

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

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

当然,在将其复制到Tables 之前,它会复制AB。问题是:我如何为 AB 创建一个指针表,这样我就可以在使用 Tables 时实际操作它们?

我希望能够使用

写入 AB
Tables[position].push_back(sub);

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

最佳答案

您必须在表格中存储 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 . . .

关于c++ - 现有对象的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42039110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com