10

I have tried every thing which i could but this code is giving me errors. Both syntax are not working. I have commented operator[] but please provide a solution for that as well.

#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;
}

while printing tuple in for loop i am getting error.

error: no matching function for call to 'std::vector<std::tuple<int, int, int> >::at(std::vector<std::tuple<int, int, int> >::const_iterator&)'

and for operator[ ]

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
  • 136
  • 1
  • 1
  • 7
  • 7
    Do not `#include `. You should include `` and `` and ``. That `bits` stuff is internal details you aren't supposed to use. – John Zwinck Oct 22 '16 at 05:50
  • Possible duplicate of [How to navigate through a vector using iterators? (C++)](http://stackoverflow.com/questions/2395275/how-to-navigate-through-a-vector-using-iterators-c) – krzaq Oct 22 '16 at 05:50

5 Answers5

12
#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;
}
Jana
  • 5,516
  • 5
  • 23
  • 29
7

why not use 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;
}

or with c++17 use the 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;
}
drBumlehund
  • 71
  • 1
  • 2
6

Your i is an iterator, which is sort of like a pointer, so you need to dereference it, not pass it to operator [] or at():

get<0>(*i);
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
2
#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;
}
0
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
  • 1,413
  • 4
  • 24
  • 37
  • the version I use Is C++ 11 – BigChief Sep 01 '21 at 22:40
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/29719970) – andygavin Sep 02 '21 at 18:20
  • this is initializing a vector of tuples, just for reference if you google "vector of tuples in c++" – BigChief Sep 29 '21 at 13:16