C中元组的向量

新手上路,请多包涵

我已经尝试了所有我能做的事情,但是这段代码给了我错误。两种语法都不起作用。我已经评论了 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 许可协议

阅读 523
1 个回答
vector<tuple<int, int>> my_vec{
    tuple<int, int> { 1, 15 },
    tuple<int, int> { 2, 100 }
};

for(const auto &i : myvec)
    cout<<get<0>(i)<<"  "<<get<1>(i)<<"  "<<get<2>(i)<<endl;

原文由 BigChief 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏