【问题标题】:bug in c++?-set_difference in c++ does not return std::copyc++ 中的错误?-c++ 中的 set_difference 不返回 std::copy
【发布时间】:2011-12-28 05:44:28
【问题描述】:

我有如下代码:

typedef std::set<std::string> set_of_strings; 
            set_of_strings s1, s2, result1; 
    some_func()
    {
            s1.insert("1-2"); 
            s1.insert("1-1"); 
            s1.insert("3-4"); 
            s2.insert("1-2"); 
            s2.insert("1-3"); 
            s2.insert("3-4"); 

            set_of_strings::iterator s1_begin = s1.begin(); 
            set_of_strings::iterator s1_end = s1.end(); 
            set_of_strings::iterator s2_begin = s2.begin(); 
            set_of_strings::iterator s2_end = s2.end(); 
            set_of_strings::iterator result_begin = result1.begin();
            td::insert_iterator<set_of_strings> result_inserter = std::inserter(result1, result_begin); 

            set_difference(s1_begin, s1_end,s2_begin, s2_end,result_inserter); //This is the problem line
}

我得到的编译错误是overloading ambiguity std::copy(.... 问题是set_difference 返回喜欢 return copy(first1,last1,result);

set_difference的算法请查看here

set_difference 返回如下:

copy(..)

如果是std::copy就没有问题了。

我尝试将我的语句放在如下块中:

{
using namespace std;
set_difference(s1_begin, s1_end,s2_begin, s2_end,result_inserter);
}

但这不起作用。 我知道问题出在我们为自己的目的编写的复制功能上,并在很多地方使用。这里我想使用std::copy。 有人可以帮忙吗。

【问题讨论】:

  • It compiles fine。所以问题出在你没有显示的代码中。 set_of_stringss1s2result1td:: 是什么?什么是完整的错误消息?
  • 那么set_difference 是在std 命名空间中定义的,它也应该使用std::copy。你能编译this code - 它显示了什么?您可以将自己的 copy 移动到不同的命名空间中吗?
  • 类型在 C++ 中很重要。你所说的一半物体我们不知道它们的类型。
  • @ybungalobill.i 刚刚给出了部分代码。复制功能是在我们的核心应用程序中定义的。我只知道我面临的问题。但我无法找到我的解决方法问题。并且类型在这里并不重要,因为我已经向您解释了我的问题。
  • 我认为 set_difference 应该是明确的并使用std::copy。与swapoperator&lt; 不同,copy 不应该是自定义点。对我来说闻起来像个虫子。

标签: c++ stl set-difference


【解决方案1】:

如果您编写了自己的复制函数,编译器可以在与std::copy 相同的范围内看到它并且它是一个可能的候选者,那么肯定会导致歧义。

没有可以设置的魔法标志让它使用std::copy,但我认为如果你将自己的副本放在命名空间中并且不using该命名空间,编译器赢了'找不到它并回退到std::copy。就是说,我认为我无法理解这样一种情况:您想要创建一个替代的 copy 用于集合迭代器,如果您编写了一个通用的,它可能不应该被称为副本,因为它会导致像这样的歧义错误没有尽头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-27
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多