繁体   English   中英

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

[英]access boost multi-index container in without iterator

抱歉,如果这是一个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;
}

我想访问例如容器中的第十个元素。 我还必须使用迭代器吗? 还是可以以类似数组的方式(或任何其他方式...请访问)访问特定的已排序元素?感谢您的帮助

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM