1

これが新しい質問である場合は申し訳ありませんが、次のコードを検討してください。

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/tokenizer.hpp>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>

using boost::multi_index_container;
using namespace boost::multi_index;


typedef multi_index_container<
  std::string,
  indexed_by<
    sequenced<>,
    ordered_non_unique<identity<std::string> >
  >
> text_container;


typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;

int main()
{
  std::string text=
    "Alice was getting very tired of sitting by her sister";

  text_container tc;
  text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-"));
  std::copy(tok.begin(),tok.end(),std::back_inserter(tc));
  int i=0;
  for(text_container::iterator bb=tc.begin();bb!=tc.end();bb++,i++)
//    std::cout << *bb << std::endl;
      std::cout << tc[i] << std::endl;
  return 0;
}

たとえば、コンテナ内の 10 番目の要素にアクセスしたいと思います。まだイテレータを使用する必要がありますか? または、配列のような方法で特定のシーケンス要素にアクセスするために離れています(または他の方法で...提案してください)

4

1 に答える 1

1

sequenced<>,行を に変更することで、マルチ インデックスのランダム アクセス インデックスを指定できますrandom_access<>,(する必要があります#include <boost/multi_index/random_access_index.hpp>)。forこれにより、ループ内の反復子を削除できます。

詳細については、ドキュメントを参照してください。

于 2012-03-27T04:25:57.980 に答える