在没有迭代器的情况下访问中的boost多索引容器
access boost multi-index container in without iterator
如果这是一个新的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
循环中的迭代器。
有关更多详细信息,请参阅文档。
相关文章:
- 为什么 Boost multi_index只修改第一个索引的键时对第二个索引进行比较?
- 在擦除或修改作为不同索引键的值时,boost::multi_index 迭代器是否无效?
- 如何从 boost::container::vector<std::string>::iterator 访问索引和对象?
- 从 boost::variant 中逐个索引获取项目,就像使用 std::variant 一样
- 如何使equal_range迭代器在 Boost 多索引中按不同的索引排序?
- C++ boost::multi_array索引太大
- 如何将值插入到 c++ boost::multiindex 集合的特定索引中,就像在 std::list 中一样
- 通过索引操作员插入boost :: program_options :: variables_map
- 如何使用 Boost V2 1.70 日志记录库在第一个索引(例如 logtrail01.txt)保留最新的日志文件名?
- 如何在不重新索引顶点的情况下调用"boost::remove_vertex"?
- 修改Boost多索引项的非索引字段的最佳方法:modify vs mutable
- 使用Boost.Preprocessor生成类模板的一组标记和索引的专门化
- 我是否需要通过锁来保护对 Boost MultiIndex 索引(索引本身)的访问?
- 存储Boost Multi_index索引迭代器
- 使用Boost Multi_multi索引
- 在Boost多索引容器中搜索位字段数据
- 获取 boost::variant 的类型索引与 boost::mpl
- boost ::几何查询返回索引
- 在Boost :: Dynamic_bitset中提取设置的位索引
- Boost::Spirit::QI解析器:已解析元素的索引