4

現在、Boost のマルチインデックスを使用して、パケットがシステムを通過する回数を追跡しています。

システムがパケットにアクセスするたびに、その IP アドレスがコンマで区切られた文字列に追加されます。次に、その文字列を調べてトークン化し、見つかった各 IP をマルチインデックスに追加します。現在、IP は一意に設定されているため、同じ IP がマルチインデックスに 2 回追加されることはありません。その場合、パケットが同じ 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);
    }
}
4

1 に答える 1

8

insert の呼び出しは std::pair< iterator, bool > を返します。ブール値は、挿入が成功した場合にのみ true になります。

http://www.boost.org/doc/libs/1_43_0/libs/multi_index/doc/reference/ord_indices.html#modifiersを参照してください。

于 2010-06-28T00:34:15.000 に答える