Reputation: 176
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> > >}')
Upvotes: 12
Views: 70862
Reputation: 1515
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;
Upvotes: 0
Reputation: 101
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;
}
Upvotes: 10
Reputation: 39
#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;
}
Upvotes: 2
Reputation: 5704
#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;
}
Upvotes: 13
Reputation: 249123
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);
Upvotes: 6