gpt4 book ai didi

c++ - std::set_difference 是否可以比较集合和映射键?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:57:28 26 4
gpt4 key购买 nike

所以我们得到了一组新的字符串,我们有一个作为映射键。我们想用一种方式 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 并以更简洁的方式进行设置?

最佳答案

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

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

关于c++ - std::set_difference 是否可以比较集合和映射键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7758239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com