提取具有lambda功能的“ std :: set”容器的差异
我想提取两组之间的差异。 当我也需要另一个容器中的结果时,这个问题提供了足够的答案。
下面的代码演示了我想要做的事情:
std::set<std::string> s1, s2, difference;
std::string difference_string = "";
std::set_difference(
s1.begin(), s1.end(), s2.begin(), s2.end(),
std::inserter(difference, difference.end()));
for (const std::string& s: difference) {
difference_string += s + ",";
}
if (0 < duplicateInPorts.size()) {
difference_string.pop_back(); //remove last comma
}
除了我发现创建局部变量不方便之外,本质上我只想有一个仅关于内容的字符串;我可以使用 lambda 轻松编译它。
std
中是否有一个方便的函数可以让我做到这一点? 我想到的语法与 std::transform
类似。
I'd like to extract the differences between two sets.
This question provides an adequate answer when I need the result in another container as well.
The following code demonstrates what I'd like to do:
std::set<std::string> s1, s2, difference;
std::string difference_string = "";
std::set_difference(
s1.begin(), s1.end(), s2.begin(), s2.end(),
std::inserter(difference, difference.end()));
for (const std::string& s: difference) {
difference_string += s + ",";
}
if (0 < duplicateInPorts.size()) {
difference_string.pop_back(); //remove last comma
}
except I find it inconvenient to create a local variable, when in essence I'd like to have a string only about the contents; Which I could easily compile with a lambda.
Is there a convenience function in std
which would enable me to do that?
What I had in mind has similar syntay as std::transform
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
std
中没有便利函数,但boost
中有一个。示例 for
boost::function_output_iterator
几乎正是您想要的。稍作修改:function_output_iterator
只是一个可调用对象的包装器,为它提供了迭代器的接口。operator*
返回一个由operator=
调用该函数的代理。There isn't a convinience function in
std
, but there is one inboost
.The example for
boost::function_output_iterator
is almost exactly what you want. Slightly adapted:And
function_output_iterator
is just a wrapper around a callable that gives it the interface of an iterator.operator*
returns a proxy who'soperator=
calls the function.