使用std :: vector <std :: pair <>>查看向量

Per*_*ulf -1 c++ oop iterator vector std-pair

我有以下向量:

std::vector< std::pair<std::string,bool > > myvec;
Run Code Online (Sandbox Code Playgroud)

如何使用迭代器浏览和打印矢量元素?

For*_*veR 7

你有什么问题?

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;
}
Run Code Online (Sandbox Code Playgroud)

或者你可以使用std::for_each一些仿函数.