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

发布于 2024-09-07 09:54:26 字数 2093 浏览 8 评论 0原文

我目前正在使用 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);
    }
}

I'm currently using Boost's multi-index to help keep track of how many times a packet passes through a system.

Each time a system touches the packet, its IP address is added to a string, separated by commas. I go through that string then, tokenize it and add each IP found to a multi-index. Since the IPs are set to be unique right now, it's impossible for the same IP to be added twice to the multi-index. What should happen then is the value associated with the IP address should be incremented, counting how many times the packet went through that same IP.

Anyways, my issue comes in here. When I use something like stl map, I will get back a response which lets me know a key could not be added due to a duplicate key already existing within the map. Does Boost's multi-index offer anything similar? I know that if I attempt to insert the same IP it will fail, but how can I tell it failed?

Here's a portion 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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-09-14 09:54:26

您对 insert 的调用返回 std::pair<迭代器,布尔>。仅当插入成功时,布尔值才会为 true。

请参阅http://www.boost。 org/doc/libs/1_43_0/libs/multi_index/doc/reference/ord_indices.html#modifiers

Your call to insert returns a std::pair< iterator, bool >. The bool will be true only if the insert succeeded.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文