-1
我有以下的载体:浏览与载体的std ::向量<性病::对< >>
std::vector< std::pair<std::string,bool > > myvec;
我怎样才能浏览和我的迭代器载体的印刷元素?
我有以下的载体:浏览与载体的std ::向量<性病::对< >>
std::vector< std::pair<std::string,bool > > myvec;
我怎样才能浏览和我的迭代器载体的印刷元素?
你的问题是什么?
typedef std::vector<std::pair<std::string, bool> > vector_type;
for (vector_type::const_iterator pos = myvec.begin();
pos != myvec.end(); ++pos)
{
std::cout << pos->first << " " << pos->second << std::endl;
}
或者您可以使用std::for_each
与一些函子。
Container::iterator iter = myContainer.begin()
)在for
-loopiterator
具有operator++
;结束条件是 - 检查是否迭代器有达到你的容器一样的end
:iter != myContainer.end()
)operator->
。std::pair
就像一个结构有两个字段 - first
和second
,因此您可以打印矢量的元素,如:iter->first
和iter->second
。
重载不起作用:两个操作数类型都在'std'命名空间中,所以重载需要放在'std'命名空间中。但是,你不能在'std'命名空间中声明重载。 – 2012-08-10 18:29:55
@JamesMcNellis你是对的。编辑。 – ForEveR 2012-08-10 18:33:47
您将'typedef'定义为'vector'并且定义了'const_iterator'当我改变了我的代码时,我得到了错误,因为我使用了'find_if'。 – PersianGulf 2012-08-10 18:34:53