В настоящее время я использую мультииндекс 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);
}
}