c ++中的错误? - c ++中的set_difference不返回std :: copy

时间:2011-12-28 05:44:29

标签: c++ stl set-difference

我有一个如下代码:

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);

请检查here以获取set_difference的算法。

set_difference返回如下:

copy(..)

如果是std::copy则不会有任何问题。

我尝试将我的陈述放在如下所示的块中:

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

但这不起作用。 我知道问题在于我们为自己的目的而编写的复制功能,并且它在很多地方使用。我想使用std::copy。 任何人都可以帮忙。

1 个答案:

答案 0 :(得分:4)

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

您可以设置使用std::copy的魔法标记,但如果您将自己的副本放在命名空间中并且不using该命名空间,我认为 ,编译器将无法找到它并回退到std::copy。也就是说,我认为我不能理解你想要创建一个适用于set迭代器的备用copy的情况,如果你写了一个通用的,它可能不应该被称为copy,因为它'这样就不会导致模糊错误的结束。