c++ 中的错误?-c++ 中的 set_difference不返回 std::copy
我有如下代码:
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 返回类似:
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 likereturn 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您编写了自己的复制函数,编译器可以在与 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'tusing
that namespace, the compiler won't be able to find it and fall back tostd::copy
. That said, I don't think I can understand a case where you would want to create an alternatecopy
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.