2016-10-22 7 views
4

私はできることをすべて試しましたが、このコードではエラーが出ています。 両方の構文が機能しません。私は演算子[]にコメントしましたが、その解決策も提供してください。C++のタプルのベクトル

#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&)' 

とあなたのiは一種のポインタのようなものですイテレータ、ですので、あなたがそれを間接参照する必要があり、operator []またはat()に渡していない

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> > >}') 
+2

'#bits/stdC++。h>'は含めないでください。 「」と「」と「」を含める必要があります。その「ビット」は、あなたが使用すべきではない内部の細部です。 –

+0

[イテレータを使ってベクトルをナビゲートする方法は? (C++)](http://stackoverflow.com/questions/2395275/how-to-navigate-through-a-vector-using-iterators-c) – krzaq

答えて

3

[]演算子のため:

get<0>(*i); 
4
#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; 
} 
関連する問題