【问题标题】:Vector of Tuples in C++C++中的元组向量
【发布时间】: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> > >}')

【问题讨论】:

标签: c++


【解决方案1】:

您的i 是一个迭代器,它有点像一个指针,所以您需要取消引用它,而不是将它传递给operator []at()

get<0>(*i);

【讨论】:

    【解决方案2】:
    #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>(*i) << endl;
            cout << get<1>(*i) << endl;
            cout << get<2>(*i) << endl;
        }
        cout << get<0>(tl[0]) << endl;
        cout << get<1>(tl[0]) << endl;
        cout << get<2>(tl[0]) << endl;
    
        return 0;
    }
    

    【讨论】:

      【解决方案3】:
      #include <bits/stdc++.h>
      using namespace std;
      int main() {
          vector<tuple<int,string,int>> vt;
          vt.push_back({421,"cha",10});
          vt.push_back({464,"sam",20});
          vt.push_back({294,"sac",30});
          for(const auto &i : vt)
              cout<<get<0>(i)<<"  "<<get<1>(i)<<"  "<<get<2>(i)<<endl;
      return 0;
      }
      

      【讨论】:

        【解决方案4】:

        为什么不使用std::tie

        #include <vector>
        #include <tuple>
        #include <iostream>
        
        int main() {
            std::vector<std::tuple<int, int, int>>  tuples;
        
            tuples.push_back(std::make_tuple(21, 20, 19));
            tuples.push_back(std::make_tuple(18, 17, 19));
        
            for (auto&& tuple: tuples)
            {
              int X, Y, Z;
              std::tie(X, Y, Z) = tuple;
        
              std::cout << X << " " << Y << " " << Z << std::endl;
            }
        
            return 0;
        }
        

        或在 c++17 中使用 auto [ var1, var2, ..., varX]

        #include <vector>
        #include <tuple>
        #include <iostream>
        
        int main() {
            std::vector<std::tuple<int, int, int>>  tuples;
        
            tuples.push_back(std::make_tuple(21, 20, 19));
            tuples.push_back(std::make_tuple(18, 17, 19));
        
            for (auto [ X, Y, Z ] : tuples)
            {
              std::cout << X << " " << Y << " " << Z << std::endl;
            }
        
            return 0;
        }
        

        【讨论】:

          【解决方案5】:
          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;
          

          【讨论】:

          • 我使用的版本是C++ 11
          • 这并没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
          • 这是初始化一个元组向量,仅供参考,如果你用谷歌搜索“c++中的元组向量”
          猜你喜欢
          • 2015-05-06
          • 2018-01-17
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多