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

发布于 2024-12-23 06:16:18 字数 1470 浏览 5 评论 0原文

我有如下代码:

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
}

我得到的编译错误是重载歧义 std::copy(.... 问题是 set_difference 返回类似 返回副本(first1,last1,result);

请检查此处 set_difference 的算法。

set_difference 返回类似:

copy(..)

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

我尝试将我的语句放入如下所示的块中:

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

但这不起作用。 我知道问题在于我们为自己的目的而编写的复制函数,并且它在很多地方使用。在这里我想使用std::copy。 有人可以帮忙吗?

I have a code like below:

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
}

The compilation error that i get is overloading ambiguity std::copy(....
the problem is set_difference returns like
return copy(first1,last1,result);

Please check here for the algo rithm of set_difference.

set_difference returns like :

copy(..)

if it is std::copy there would not be any problem.

i tried with putting my statement inside a block like below:

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

but this doesn't work.
I know that the problem is with the copy function which we have written for our own purpose and its used at many places.Here i want to use std::copy.
could anybody pls help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

失而复得 2024-12-30 06:16:18

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

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

If you've written your own copy function that the compiler can see in the same scope as std::copy and it's a possible candidate, then sure that would cause an ambiguity.

There's no magic flag you can set to make it use std::copy, but I think if you put your own copy in a namespace and don't using that namespace, the compiler won't be able to find it and fall back to std::copy. That said, I don't think I can understand a case where you would want to create an alternate copy that works for set iterators, and if you wrote a generic one it probably shouldn't be called copy because it'll cause no end of ambiguity errors like this one.

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