std::set_difference 是否可以比较集合和映射键?
所以我们得到了一组新的字符串,并且有一个作为映射键。我们想要单向
set_difference(注意 - 不是 set_symmetry_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 并以更干净的方式设置?
So we get a new set of strings, and we have one as map Keys. And we want to do one way
set_difference (note - not set_symmetric_difference). So currently I have such ugly code like:
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;
}
I wonder how to do set_difference over map Keys and set in more clean way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的:您可以使用转换迭代器仅迭代
std::map
的键。您可以在 我对另一个问题提供的答案。
Yes: You can iterate over just the keys of the
std::map
using a transform iterator.You can find two implementations of such a transform iterator (one using Boost, the other standalone) in an answer I provided to another question.