0

我有一个关于提升多索引中的序列索引的非常简单的问题。代码如下:

我的类存储在 Link.hpp 中:

#include <string>

class Link  {
public:
    Link(std::string l,std::string r) :linkID(l),roadName(r) {}
    Link() {}
    std::string roadName;
    std::string linkID;

};

和主要功能:

#include "Link.hpp"
#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/key_extractors.hpp>

using boost::multi_index::multi_index_container;
using boost::multi_index::ordered_non_unique;
using boost::multi_index::ordered_unique;
using boost::multi_index::sequenced;
using boost::multi_index::indexed_by;
using boost::multi_index::member;

typedef boost::multi_index::multi_index_container<Link,
        indexed_by<sequenced<> > > Links;

int main() {
    Links Ls;
    Ls.insert(Link("123", "456"));

    return 1;
}

编译,当 sequenced<> 存在时,会产生一个我不明白的错误。你能帮我吗?

错误:

$ g++ Links.cpp 
Links.cpp: In function ‘int main()’:
Links.cpp:29:29: error: no matching function for call to ‘boost::multi_index::multi_index_container<Link, boost::multi_index::indexed_by<boost::multi_index::sequenced<> > >::insert(Link)’
Links.cpp:29:29: note: candidates are:
/usr/include/boost/multi_index/sequenced_index.hpp:267:28: note: std::pair<boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::sequenced_index_node<typename SuperMeta::type::node_type> >, bool> boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::insert(boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::iterator, boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::value_param_type) [with SuperMeta = boost::multi_index::detail::nth_layer<1, Link, boost::multi_index::indexed_by<boost::multi_index::sequenced<> >, std::allocator<Link> >, TagList = boost::mpl::vector0<mpl_::na>, typename SuperMeta::type::node_type = boost::multi_index::detail::index_node_base<Link, std::allocator<Link> >, boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::iterator = boost::multi_index::detail::bidir_node_iterator<boost::multi_index::detail::sequenced_index_node<boost::multi_index::detail::index_node_base<Link, std::allocator<Link> > > >, boost::multi_index::detail::sequenced_index<SuperMeta, TagList>::value_param_type = const Link&]
/usr/include/boost/multi_index/sequenced_index.hpp:267:28: note:   candidate expects 2 arguments, 1 provided...................
4

2 回答 2

3

insert期望迭代器作为第一个参数来指定要在容器中插入的位置。它似乎multi_index_container也有push_back,这可能更接近你想要的。

所以要在序列的开头插入:

Ls.insert( Ls.begin(), Link( "123", "456" ) );

或者在它的后面:

Ls.push_back( Link( "123", "456" ) );
于 2012-03-26T08:34:22.433 回答
0

它在较新版本的 boost 中工作,其中 insert 只需要一个参数,即要插入的值。同时, push_back 方法被移除。

于 2020-07-30T00:45:51.213 回答