私は次のベクトルを持っています:
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
-loopで、すべての要素を反復処理します(has iterator
;operator++
終了条件は-イテレータがend
コンテナのに到達したかどうかを確認しiter != myContainer.end()
ます。operator->
。std::pair
は2つのフィールドを持つ構造体のようなものです-first
と。したがって、:と。second
のようなベクトルの要素を出力できます。iter->first
iter->second