2011-01-27 8 views
1

次のコードでイテレータiを宣言する適切な方法は何ですか?入れ子になったコンテナのイテレータをtypedefするには?

#include <iostream> 
#include <vector> 

using namespace std; 

template<class Mat> 
void f(const Mat& mat) 
{ 
    typedef typename Mat::value_type::iterator itr; 
    //itr i = (mat.begin())->begin(); //This Line Gives an error 
    typeof((mat.begin())->begin()) i = (mat.begin())->begin(); 
} 

int main() 
{ 
    vector<vector<int> > vvi; 
    f(vvi); 
    return 0; 
} 
+2

エラーは? – user470379

+0

constを使用しないエラーです。私はエラーラインからこれを理解することができませんでした。 – balki

答えて

3

それをSTLの方法を行うと、イテレータを渡す:

template<class Mat> 
void f(const Mat& mat) 
{ 
    typedef typename Mat::value_type::const_iterator itr; 

    itr i = mat.begin()->begin(); 
} 
+1

+1もちろん、ああ。ずっといい。 :) – GManNickG

+0

cont :: namespaceはどこから来ますか? – fabrizioM

+0

@ fabrizioM: 'typedef typename std :: iterator_traits :: value_type cont;' – sbi

2

コンテナはconstですが、イテレータタイプはありません。 、

typedef typename Mat::value_type::const_iterator itr; 
itr i = (mat.begin())->begin(); 
1

のconstイテレータをお試しください:

typedef typename Mat::value_type::const_iterator itr; 
1

あなたはconstの&として渡すので、あなたneed a const_iteratorconst_iteratorがいることを確認してください容器なし:

//Beware, brain-compiled code ahead! 
template<typename It> 
void f(It begin, It end) 
{ 
    typedef typename std::iterator_traits<It>::value_type cont; 
    typedef typename cont::const_iterator const_iterator; // note the const_ pfx 
    const_iterator i = begin->begin(); 
    // ... 
} 

int main() 
{ 
    vector<vector<int> > vvi; 
    f(vvi.begin(), vvi.end()); 
    return 0; 
} 
関連する問題