如何typedef一个嵌套容器的迭代器? [英] How to typedef the iterator of a nested container?
本文介绍了如何typedef一个嵌套容器的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下代码中声明迭代器i的正确方法是什么?
What is the proper way to declare the iterator i in the following code?
#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;
}
推荐答案
传递迭代器,而不是容器:
Do it the STL way and pass iterators, not containers:
//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;
}
这篇关于如何typedef一个嵌套容器的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文