C++ 多索引插入失败返回(Boost)

C++ 多索引插入失败返回(Boost),c++,boost,return-value,multi-index,C++,Boost,Return Value,Multi Index,我目前正在使用Boost的多索引来帮助跟踪数据包通过系统的次数 每次系统接触数据包时,其IP地址都会添加到一个字符串中,并用逗号分隔。然后,我遍历该字符串,标记它,并将找到的每个IP添加到一个多索引中。由于IP现在被设置为唯一,因此不可能将同一IP添加到多索引中两次。然后,应该发生的是与IP地址相关联的值应该递增,计算数据包通过同一IP的次数 不管怎样,我的问题来了。当我使用类似stl映射的东西时,我会得到一个响应,它让我知道由于映射中已经存在重复的密钥,所以无法添加密钥。Boost的多重指数是

我目前正在使用Boost的多索引来帮助跟踪数据包通过系统的次数

每次系统接触数据包时,其IP地址都会添加到一个字符串中,并用逗号分隔。然后,我遍历该字符串,标记它,并将找到的每个IP添加到一个多索引中。由于IP现在被设置为唯一,因此不可能将同一IP添加到多索引中两次。然后,应该发生的是与IP地址相关联的值应该递增,计算数据包通过同一IP的次数

不管怎样,我的问题来了。当我使用类似stl映射的东西时,我会得到一个响应,它让我知道由于映射中已经存在重复的密钥,所以无法添加密钥。Boost的多重指数是否提供了类似的服务?我知道,如果我尝试插入相同的IP,它将失败,但我如何知道它失败了

以下是我当前代码的一部分:

// Multi-index handling
using boost::multi_index_container;
using namespace boost::multi_index;

struct pathlog
{
    string         hop;
    int     passedthru;

    pathlog(std::string hop_,int passedthru_):hop(hop_),passedthru(passedthru_){}

    friend std::ostream& operator<<(std::ostream& os,const pathlog& e)
    {
        os<<e.hop<<" "<<e.passedthru<<std::endl;
        return os;
    }
};

// structs for data
struct hop{};
struct passedthru{};

// multi-index container setup
typedef multi_index_container<
pathlog,
indexed_by<
ordered_unique<
tag<hop>,  BOOST_MULTI_INDEX_MEMBER(pathlog,std::string,hop)>,
ordered_non_unique<
tag<passedthru>, BOOST_MULTI_INDEX_MEMBER(pathlog,int,passedthru)> >
> pathlog_set;


int disassemblepathlog(const string& str, pathlog_set& routecontainer, const string& delimiters = ","){
    // Tokenizer (heavily modified) from http://oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html

    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        routecontainer.insert(pathlog((str.substr(lastPos, pos - lastPos)),1)); // if this fails, I need to increment the counter!
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}
//多索引处理
使用boost::multi_index_容器;
使用名称空间boost::multi_索引;
结构路径日志
{
弦乐跳;
int-passedthru;
路径日志(std::string-hop,int-passedthru):hop(hop),passedthru(passedthru){

friend std::ostream&operator对insert的调用返回一个std::pair。只有在insert成功时,bool才会为真


请参见

我认识到我可以先搜索我要添加的IP,然后(如果它不存在)添加它。如果可能的话,我只希望将事情保持在最紧凑的操作中。