2010-06-28 48 views
3

我目前使用Boost的多索引來幫助跟蹤數據包通過系統的次數。多索引插入失敗返回(增強)

每次系統接觸數據包時,其IP地址都會添加到一個字符串中,並用逗號分隔。然後,我瀏覽該字符串,標記它並將每個找到的IP添加到多索引中。由於IP現在被設置爲唯一,所以相同的IP不可能被添加兩次到多索引。 應該發生然後是與IP地址相關聯的值應增加,計算數據包通過同一IP的次數。

無論如何,我的問題來到這裏。當我使用類似stl map的東西時,我會回覆一個響應,讓我知道由於地圖中已經存在重複鍵,無法添加鍵。 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); 
    } 
} 
+0

我承認我能爲我加入,然後(如果不存在的話),添加它的IP首先做一個搜索。如果可能的話,我只想讓事情保持最緊湊的狀態。 – BSchlinker 2010-06-28 00:15:30

回答