如何调用 boost multi_index元素的非常量成员函数
how to call non-constant member function of a boost multi_index element
我发布这个是因为我有点不明白提升教程是如何工作的。
我有一个类,其对象是提升multi_index容器的元素。
我需要使用成员函数更新对象的成员变量。我不知道该怎么做。你能帮帮我吗?我准备了一个简单的例子:
#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include<vector>
using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::member;
class employee_entry
{
public:
employee_entry( const std::string& first,
const std::string& last,
long id):
first_name_(first),
last_name_(last),
id_(id)
{}
void change(){id_++;}//causing the problem
std::string first_name_;
std::string last_name_;
std::vector<int> mySet;
long id_;
std::vector<int>::iterator mySet_begin() {return mySet.begin(); }
};
typedef multi_index_container<
employee_entry, indexed_by<
ordered_unique<member<employee_entry, std::string, &employee_entry::first_name_> >
, ordered_non_unique<member<employee_entry, std::string, &employee_entry::last_name_> >
, ordered_non_unique<member<employee_entry, long, &employee_entry::id_> >
>
> employee_set;
//employee set.... multi-index
employee_set m_employees;
int main()
{
using boost::multi_index::nth_index;
using boost::multi_index::get;
typedef nth_index<employee_set, 0>::type first_name_view;
first_name_view& fnv = get<0>(m_employees);
fnv.insert(employee_entry("John", "Smith", 110));
fnv.insert(employee_entry("Fudge", "Hunk", 97));
///get employees sorted by id
typedef nth_index<employee_set, 2>::type id_view;
id_view& idv = get <2> (m_employees);
for(id_view::reverse_iterator it = idv.rbegin(), it_end(idv.rend()); it != it_end; ++it)
{
std::cout << it->first_name_ <<" "
<< it->last_name_ << ":"
<< it->id_ << std::endl;
it->change();//calling the troublesome function
}
return 0;
}
生成的错误是:
$c++ dr_function.cpp
dr_function.cpp: In function ‘int main()’:
dr_function.cpp:65:19: error: passing ‘const employee_entry’ as ‘this’ argument of ‘void employee_entry::change()’ discards qualifiers [-fpermissive]
您发布的解决方案将不起作用:充其量它会得到一个乱码索引,最坏的情况是您的应用程序会崩溃。由于您的最后一个索引取决于employee_entry::id_
,因此您无法随意更改它,因为您隐式地破坏了索引顺序。对于这类东西,Boost.MultiIndex 提供了更新函数replace
和modify
,如此处所述。在特定情况下,可以按如下方式调用 change
成员函数:
idv.modify(idv.iterator_to(*it),boost::bind(&employee_entry::change,_1));
一点解释:idv.iterator_to(*it)
只是将反向迭代器转换为常规迭代器,这正是modify
所需要的。至于boost::bind
部分,这会将&employee_entry::change
封装到合适的修改函子中。这样,您就可以让 Boost.MultiIndex 了解即将发生的id_
变化,并相应地更新索引。
好吧,我至少要回答我的问题来调试上述代码。我的主要担忧在问题下面的评论中得到了讨论和回答。我使用以下代码,我mutable
long id_
,并将void change(){...}
更改为void change()const{...}
#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include<vector>
using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::indexed_by;
using boost::multi_index::member;
class employee_entry
{
public:
employee_entry( const std::string& first,
const std::string& last,
long id):
first_name_(first),
last_name_(last),
id_(id)
{}
void change() const {id_++;}//causing the problem
std::string first_name_;
std::string last_name_;
std::vector<int> mySet;
mutable long id_;
std::vector<int>::iterator mySet_begin() {return mySet.begin(); }
};
typedef multi_index_container<
employee_entry, indexed_by<
ordered_unique<member<employee_entry, std::string, &employee_entry::first_name_> >
, ordered_non_unique<member<employee_entry, std::string, &employee_entry::last_name_> >
, ordered_non_unique<member<employee_entry, long, &employee_entry::id_> >
>
> employee_set;
//employee set.... multi-index
employee_set m_employees;
int main()
{
using boost::multi_index::nth_index;
using boost::multi_index::get;
typedef nth_index<employee_set, 0>::type first_name_view;
first_name_view& fnv = get<0>(m_employees);
fnv.insert(employee_entry("John", "Smith", 110));
fnv.insert(employee_entry("Fudge", "Hunk", 97));
///get employees sorted by id
typedef nth_index<employee_set, 2>::type id_view;
id_view& idv = get <2> (m_employees);
for(id_view::reverse_iterator it = idv.rbegin(), it_end(idv.rend()); it != it_end; ++it)
{
std::cout << it->first_name_ <<" "
<< it->last_name_ << ":"
<< it->id_ << std::endl;
it->change();//calling the troublesome function
}
return 0;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 在提升multi_index容器中,是否定义了"default index"?
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 通过迭代器获取 set 元素的"index"