我已经尝试了所有我能做的事情,但是这段代码给了我错误。两种语法都不起作用。我已经评论了 operator[] 但也请为此提供解决方案。
#include <bits/stdc++.h>
using namespace std;
int main() {
typedef vector< tuple<int, int, int> > my_tuple;
my_tuple tl;
tl.push_back( tuple<int, int, int>(21,20,19) );
for (my_tuple::const_iterator i = tl.begin(); i != tl.end(); ++i) {
//cout << get<0>(tl[i]);
//cout << get<0>(tl[i]);
//cout << get<0>(tl[i]);
cout << get<0>(tl.at(i));
cout << get<1>(tl.at(i));
cout << get<2>(tl.at(i));
}
return 0;
}
在 for 循环中打印元组时出现错误。
error: no matching function for call to 'std::vector<std::tuple<int, int, int> >::at(std::vector<std::tuple<int, int, int> >::const_iterator&)'
对于运算符 [ ]
error: no match for 'operator[]' (operand types are 'my_tuple {aka std::vector<std::tuple<int, int, int> >}' and 'std::vector<std::tuple<int, int, int> >::const_iterator {aka __gnu_cxx::__normal_iterator<const std::tuple<int, int, int>*, std::vector<std::tuple<int, int, int> > >}')
原文由 Shailendra Gupta 发布,翻译遵循 CC BY-SA 4.0 许可协议