【问题标题】:std::set_difference is it possible to compare set and map Keys?std::set_difference 是否可以比较 set 和 map 键?
【发布时间】:2011-10-13 17:49:08
【问题描述】:

所以我们得到了一组新的字符串,我们有一个作为映射键。我们想做one way set_difference(注意 - 不是 set_symmetric_difference)。所以目前我有这样丑陋的代码:

    std::map<std::string, boost::shared_ptr<some_class> > _ds;
std::set<std::string> compare_and_clean(std::set<std::string> &new_)
{
    std::set<std::string> result;
    std::set<std::string> old;

    for (std::map<std::string, std::string>::iterator mi = _ds.begin(); mi != _ds.end(); ++mi)
        old.insert(mi->first);

    std::set_difference( old.begin(), old.end(), new_.begin(), new_.end(), inserter(result, result.begin()));

    for (std::set<std::string>::iterator i = result.begin(); i != result.end(); ++i) {
        _ds.erase(*i);
    }
    return result;
}

我想知道如何在地图键上进行 set_difference 并以更干净的方式设置?

【问题讨论】:

  • “一旦我找到真正的答案并将其标记为正确答案,我就会向所有试图回答我的问题的人投赞成票。”请只为有用的答案投票。

标签: c++ map set


【解决方案1】:

是的:您可以使用转换迭代器仅迭代 std::map 的键。

您可以在an answer I provided to another question 中找到这种转换迭代器的两种实现(一种使用 Boost,另一种独立)。

【讨论】:

    猜你喜欢
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2014-06-06
    • 2010-11-06
    相关资源
    最近更新 更多