我正在尝试创建一个包含元组的 vector
这是我的代码:
std::vector<std::tuple<std::vector<std::tuple<std::string,int,int>>> vec;
...
...
for (i = 0; i < 5; i++) {
vec.push_back(std::tuple<std::vector<std::tuple<std::string,int,int>>());
for(int j=0;j<5;j++) {
std::get<0>(vec[i]).push_back(std::tuple<std::string,int,int>());
std::get<0>(std::get<0>(vec[i]))[j] = value; //error
std::get<1>(std::get<0>(vec[i]))[j] = value1;
}
错误:
no matching function for call to 'get(std::vector<std::tuple<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int, int> >&)'*
它有什么问题? 谢谢。
最佳答案
这是你的代码的工作版本
std::vector<std::tuple<std::vector<std::tuple<std::string,int,int>>>> vec;
for (int i = 0; i < 5; i++) {
vec.push_back(std::tuple<std::vector<std::tuple<std::string,int,int>>>());
for(int j=0;j<5;j++) {
std::get<0>(vec[i]).push_back(std::tuple<std::string,int,int>());
std::get<0>(std::get<0>(vec[i])[j]) = "str";
std::get<1>(std::get<0>(vec[i])[j]) = 1;
}
}
http://coliru.stacked-crooked.com/a/0cdc4531904a56b3
您缺少一些 >
,并且您完全搞砸了赋值语句。
我的建议:永远不要创建这样的数据结构。如果您需要复杂的数据结构,请制作真正的结构/类。给他们有意义的名字。这将使代码更具可读性和可维护性。
例如
struct Record {
std::string name;
int value;
int otherValue;
};
代替
std::tuple<std::string,int,int>
还有一个元组只包含单个元素,这在这里没有多大意义。
关于c++ - std::get 在处理元组 vector 时返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707400/