1

抱歉,如果这是一个 newB 问题,请考虑以下代码:

#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 个元素。我还必须使用迭代器吗?或者是否可以以类似数组的方式访问特定的序列元素(或任何其他方式......请建议)感谢您的帮助 vahid

4

1 回答 1

1

sequenced<>,您可以通过将行更改为random_access<>,(您需要)来为多索引指定随机访问索引#include <boost/multi_index/random_access_index.hpp>。这将允许您删除for循环中的迭代器。

有关更多详细信息,请参阅文档

于 2012-03-27T04:25:57.980 回答