【发布时间】: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_strings、s1、s2、result1和td::是什么?什么是完整的错误消息? -
那么
set_difference是在std命名空间中定义的,它也应该使用std::copy。你能编译this code - 它显示了什么?您可以将自己的copy移动到不同的命名空间中吗? -
类型在 C++ 中很重要。你所说的一半物体我们不知道它们的类型。
-
@ybungalobill.i 刚刚给出了部分代码。复制功能是在我们的核心应用程序中定义的。我只知道我面临的问题。但我无法找到我的解决方法问题。并且类型在这里并不重要,因为我已经向您解释了我的问题。
-
我认为 set_difference 应该是明确的并使用
std::copy。与swap和operator<不同,copy不应该是自定义点。对我来说闻起来像个虫子。
标签: c++ stl set-difference