【发布时间】:2017-03-04 11:33:01
【问题描述】:
我已经尝试了所有我能做的事情,但是这段代码给了我错误。 两种语法都不起作用。我已经评论了 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> > >}')
【问题讨论】:
-
不要
#include <bits/stdc++.h>。您应该包括<vector>和<tuple>和<iostream>。bits的东西是你不应该使用的内部细节。
标签: c++