在没有迭代器的情况下访问中的boost多索引容器



如果这是一个新的B问题,很抱歉,请输入以下代码:

#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>(" tn.,;:!?'"-"));
  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

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

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

相关内容

最新更新