【问题标题】:access boost multi-index container in without iterator在没有迭代器的情况下访问 boost 多索引容器
【发布时间】:2012-03-27 02:26:08
【问题描述】:

对不起,如果这是一个新问题, 请考虑以下代码:

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

【问题讨论】:

    标签: c++ boost boost-multi-index


    【解决方案1】:

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

    更多详情,请参阅the documentation

    【讨论】:

    • 是的,对于 for 循环,类似这样的东西工作得很好: for(i=0;i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2017-05-07
    相关资源
    最近更新 更多