迭代和修改 boost::multiindex 的数据
Iterate and modify the data of boost::multiindex
#include "modify_example.hpp"
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <iostream>
#include <string>
using namespace boost::multi_index;
namespace{
struct name{};
struct email{};
struct employee
{
std::string email_;
std::string name_;
employee(std::string name, std::string email) :
email_(email),
name_(name)
{}
};
using DispatchUsersTable =
multi_index_container
<
employee,
indexed_by
<
ordered_unique
<
tag<name>, member
<employee, std::string, &employee::name_>
>,
ordered_unique
<
tag<email>, member
<employee, std::string, &employee::email_>
>
>
>;
}
void order_after_modify()
{
DispatchUsersTable table;
table.insert(employee{"jk", "http:cat"});
table.insert(employee{"otaku", "http:joker"});
table.insert(employee{"cba", "http:amota"});
//auto will make codes much easier to read
//I type the type explicitly because I need to
//maintain the codes created by c++98
using Table = DispatchUsersTable::index<name>::type;
using Iterator = Table::iterator;
std::vector<std::string> names{"doremi", "onpu", "aichan"};
size_t num = 0;
for(Iterator it = table.get<name>().begin();
it != table.get<name>().end(); ){
std::cout<<it->name_<<", "<<it->email_<<std::endl;
name_index.modify(it++, [&](employee &e){ e.name_ = names[num++]; });
}
}
我想通过 for 循环修改multi_index的所有内容但是代码无法正常工作(随机行为(,我认为当我将修改操作和迭代组合在一起时,我没有得到正确的迭代器。正确的方法是什么?
问题是modify
在此过程中更改元素的顺序,因此迭代可能会以相当不可预测的方式重新访问或跳过过去的元素。解决方案是在进行修改之前存储迭代器:
void order_after_modify()
{
DispatchUsersTable table;
table.insert(employee{"jk", "http:cat"});
table.insert(employee{"otaku", "http:joker"});
table.insert(employee{"cba", "http:amota"});
//auto will make codes much easier to read
//I type the type explicitly because I need to
//maintain the codes created by c++98
using Table = DispatchUsersTable::index<name>::type;
using Iterator = Table::iterator;
Table& name_index= table.get<name>();
std::vector<std::string> names{"onpu", "doremi", "aichan"};
std::vector<Iterator> view;
for(Iterator it = table.get<name>().begin();it != table.get<name>().end(); ++it){
view.push_back(it);
}
for(size_t num = 0;num<view.size();++num){
std::cout<<view[num]->name_<<", "<<view[num]->email_<<std::endl;
name_index.modify(view[num], [&](employee &e){ e.name_ = names[num]; });
}
}
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 迭代和修改 boost::multiindex 的数据