Return (increase) an insert with multiple indexes

I am currently using Boost's multi-index to track how many times a packet goes through the system.

Each time the system touches a packet, its IP address is added to the line, separated by commas. Then I look at this line and then add it and add each IP address to the multi-index. Since IP addresses are set as unique right now, it is not possible for the same IP to be added twice to a multi-index. What should , then the value associated with the IP address should increase, counting how many times the packet went through the same IP address.

Anyway, my problem is here. When I use something like a stl card, I will return an answer that lets me know that the key cannot be added due to a duplicate key already existing on the card. Does Multi-index Boost offer something like this? I know that if I try to insert the same IP address, this will not work, but how can I say that it failed?

Here is part of my current code:

// 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);
    }
}
+3
source share
1 answer

Your call to insert returns std :: pair <iterator, bool>. Bool will be true only if the insert is successful.

See http://www.boost.org/doc/libs/1_43_0/libs/multi_index/doc/reference/ord_indices.html#modifiers

+6

Source: https://habr.com/ru/post/1752017/


All Articles