提取具有lambda功能的“ std :: set”容器的差异

发布于 2025-01-21 02:18:39 字数 735 浏览 2 评论 0原文

我想提取两组之间的差异。 当我也需要另一个容器中的结果时,这个问题提供了足够的答案。

下面的代码演示了我想要做的事情:

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 技术交流群。

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

发布评论

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

评论(1

苦笑流年记忆 2025-01-28 02:18:39

std 中没有便利函数,但 boost 中有一个。

示例 for boost::function_output_iterator 几乎正是您想要的。稍作修改:

struct string_joiner
{
    string_joiner(std::string& s)
        : m_str(s)
    {}

    void operator()(const std::string& x) const
    {
        m_str += m_sep;
        m_str += x;
        m_sep = ",";
    }

    std::string& m_str;
    std::string mutable m_sep;
};

int main(int, char*[])
{
  std::set<std::string> s1, s2;
  std::string difference = "";

  std::set_difference(
    s1.begin(), s1.end(),
    s2.begin(), s2.end(),
    boost::make_function_output_iterator(string_joiner(difference)));

  std::cout << difference << std::endl;

  return 0;
}

function_output_iterator 只是一个可调用对象的包装器,为它提供了迭代器的接口。 operator* 返回一个由 operator= 调用该函数的代理。

There isn't a convinience function in std, but there is one in boost.

The example for boost::function_output_iterator is almost exactly what you want. Slightly adapted:

struct string_joiner
{
    string_joiner(std::string& s)
        : m_str(s)
    {}

    void operator()(const std::string& x) const
    {
        m_str += m_sep;
        m_str += x;
        m_sep = ",";
    }

    std::string& m_str;
    std::string mutable m_sep;
};

int main(int, char*[])
{
  std::set<std::string> s1, s2;
  std::string difference = "";

  std::set_difference(
    s1.begin(), s1.end(),
    s2.begin(), s2.end(),
    boost::make_function_output_iterator(string_joiner(difference)));

  std::cout << difference << std::endl;

  return 0;
}

And function_output_iterator is just a wrapper around a callable that gives it the interface of an iterator. operator* returns a proxy who's operator= calls the function.

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