我可以通过从modify
获得的迭代器在boost::multi_index_container
上调用get<0>().find()
。但是,当我尝试在get<1>().find()
上执行相同操作时,我收到编译错误。
是否可以通过第二个索引上的迭代器进行修改?
我正在使用g++4.9.2
和boost 1.55
。
代码是
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
using std::string;
using boost::multi_index::indexed_by;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::member;
struct Person
{
string FamilyName;
string PersonalName;
// string MiddleName;
};
typedef boost::multi_index_container
<
Person,
indexed_by<
ordered_non_unique<member<Person, string, &Person::FamilyName>>,
ordered_non_unique<member<Person, string, &Person::PersonalName>>
>
>
PersonList;
main()
{
PersonList people;
{
string toFind = "Jones";
string toReplace = "Smith";
PersonList::nth_index<0>::type::const_iterator itr0( people.get<0>().find(toFind) );
const PersonList::nth_index<0>::type::const_iterator end0(people.get<0>().cend());
for( ; itr0!= end0; itr0++)
{
people.modify(itr0, [&](Person &e){ e.FamilyName = toReplace; });
}
}
{
string toFind = "Fred";
string toReplace = "Barney";
PersonList::nth_index<1>::type::const_iterator itr1( people.get<1>().find(toFind) );
const PersonList::nth_index<1>::type::const_iterator end1(people.get<1>().cend());
for( ; itr1!= end1; itr1++)
{
people.modify(itr1, [&](Person &e){ e.PersonalName = toReplace; });
}
}
}
,错误是
LANG=C g++ -std=c++11 boost-test.cpp
boost-test.cpp: In function 'int main()':
boost-test.cpp:57:82: error: no matching function for call to 'boost::multi_index::multi_index_container<Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > > >::modify(boost::multi_index::detail::ordered_index<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName>, std::less<std::basic_string<char> >, boost::multi_index::detail::nth_layer<2, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_non_unique_tag>::const_iterator&, main()::<lambda(Person&)>)'
people.modify(itr1, [&](Person &e){ e.PersonalName = toReplace; });
boost-test.cpp:57:82: note: candidates are:
In file included from boost-test.cpp:4:0:
/usr/include/boost/multi_index/ordered_index.hpp:413:8: note: template<class Modifier> bool boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::modify(boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::iterator, Modifier) [with Modifier = Modifier; KeyFromValue = boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName>; Compare = std::less<std::basic_string<char> >; SuperMeta = boost::multi_index::detail::nth_layer<1, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_non_unique_tag]
bool modify(iterator position,Modifier mod)
^
/usr/include/boost/multi_index/ordered_index.hpp:413:8: note: template argument deduction/substitution failed:
boost-test.cpp:57:82: note: cannot convert 'itr1' (type 'boost::multi_index::detail::ordered_index<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName>, std::less<std::basic_string<char> >, boost::multi_index::detail::nth_layer<2, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_non_unique_tag>::const_iterator {aka boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<Person, std::allocator<Person> > > >}') to type 'boost::multi_index::detail::ordered_index<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName>, std::less<std::basic_string<char> >, boost::multi_index::detail::nth_layer<1, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_non_unique_tag>::iterator {aka boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<Person, std::allocator<Person> > > > >}'
people.modify(itr1, [&](Person &e){ e.PersonalName = toReplace; });
^
In file included from boost-test.cpp:4:0:
/usr/include/boost/multi_index/ordered_index.hpp:434:8: note: template<class Modifier, class Rollback> bool boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::modify(boost::multi_index::detail::ordered_index<KeyFromValue, Compare, SuperMeta, TagList, Category>::iterator, Modifier, Rollback) [with Modifier = Modifier; Rollback = Rollback; KeyFromValue = boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName>; Compare = std::less<std::basic_string<char> >; SuperMeta = boost::multi_index::detail::nth_layer<1, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >; TagList = boost::mpl::vector0<mpl_::na>; Category = boost::multi_index::detail::ordered_non_unique_tag]
bool modify(iterator position,Modifier mod,Rollback back)
^
/usr/include/boost/multi_index/ordered_index.hpp:434:8: note: template argument deduction/substitution failed:
boost-test.cpp:57:82: note: cannot convert 'itr1' (type 'boost::multi_index::detail::ordered_index<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName>, std::less<std::basic_string<char> >, boost::multi_index::detail::nth_layer<2, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_non_unique_tag>::const_iterator {aka boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<Person, std::allocator<Person> > > >}') to type 'boost::multi_index::detail::ordered_index<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName>, std::less<std::basic_string<char> >, boost::multi_index::detail::nth_layer<1, Person, boost::multi_index::indexed_by<boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::FamilyName> >, boost::multi_index::ordered_non_unique<boost::multi_index::member<Person, std::basic_string<char>, &Person::PersonalName> > >, std::allocator<Person> >, boost::mpl::vector0<mpl_::na>, boost::multi_index::detail::ordered_non_unique_tag>::iterator {aka boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::ordered_index_node<boost::multi_index::detail::index_node_base<Person, std::allocator<Person> > > > >}'
people.modify(itr1, [&](Person &e){ e.PersonalName = toReplace; });
^
答案 0 :(得分:2)
替换
people.modify(itr1, [&](Person &e){ e.PersonalName = toReplace; });
与
people.get<1>().modify(itr1, [&](Person &e){ e.PersonalName = toReplace; });