实现平等的算法 [英] Implementing equal algorithm
问题描述
所有我想要做的是实现平等的算法。但是,当我测试用弦数,我得到一个含糊不清的错误。我觉得编译器不能在A与B之间区别这是为什么?
模板< A类,B>布尔等于(A求,A端,B OUT)
{
而(求!=结束){
如果(*求== *出){
++求;
++出来;
}
否则返回false;
}
返回true;
}
主
的std ::字符串(这是一个字符串);
标准::字符串B(这是一个字符串);
性病::串c(字符串C);
性病::法院<< a和b是&其中;&其中;等于(a.begin(),a.end(),b.begin())≤;&其中;的std :: ENDL;
性病::法院<< a和c&其中;&其中;等于(a.begin(),a.end(),c.begin())≤;&其中;的std :: ENDL;
错误信息
procedures_main.cpp:17:35:错误:调用'等于'不明确
性病::法院<< a和b是&其中;&其中;等于(a.begin(),a.end(),b.begin())≤;&其中;的std :: ENDL;
^ ~~~~
/Applications/X$c$c.app/Contents/Developer/Toolchains/X$c$cDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:1105:1:注意:
候选函数[与_InputIterator1 =的std :: __ 1 :: __ wrap_iter<字符*>中_InputIterator2 =
的std :: __ 1 :: __ wrap_iter<字符*>]
等于(_InputIterator1 __first1,_InputIterator1 __last1,_InputIterator2 __first2)
^
./procedures.hpp:73:34:注:候选函数[有A =的std :: __ 1 :: __ wrap_iter<字符*>中B =的std :: __ 1 :: __ wrap_iter<焦炭
*>]
模板< A类,B>布尔等于(A求,A端,B OUT)
现在的问题是,该参数(由的std ::字符串
的迭代器)的命名空间 STD
在这个命名空间中,还有另一种算法叫做等于
这是一个候选由于参数依赖查找(ADL)。你需要明确限定你的算法:
性病::法院<< a和b是&其中;&其中; ::相等(a.begin(),a.end(),b.begin())≤;&其中;的std :: ENDL;
// ^^这里
请注意,在C ++标准不要求迭代器是在 STD
A型,但允许的它,你的编译器/标准库决定使用这个选项。
All I'm trying to do is implement the equal algorithm. However, when I test with a few strings, I get an ambiguity error. I think the compiler cannot differentiate between the A and the B. Why is this?
template <class A, class B> bool equal(A beg, A end, B out)
{
while(beg != end) {
if(*beg == *out) {
++beg;
++out;
}
else return false;
}
return true;
}
MAIN
std::string a("This is a string");
std::string b("This is a string");
std::string c("String c");
std::cout << "a and b are " << equal(a.begin(), a.end(), b.begin()) << std::endl;
std::cout << "a and c are " << equal(a.begin(), a.end(), c.begin()) << std::endl;
ERROR MESSAGE
procedures_main.cpp:17:35: error: call to 'equal' is ambiguous
std::cout << "a and b is " << equal(a.begin(), a.end(), b.begin()) << std::endl;
^~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:1105:1: note:
candidate function [with _InputIterator1 = std::__1::__wrap_iter<char *>, _InputIterator2 =
std::__1::__wrap_iter<char *>]
equal(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2)
^
./procedures.hpp:73:34: note: candidate function [with A = std::__1::__wrap_iter<char *>, B = std::__1::__wrap_iter<char
*>]
template <class A, class B> bool equal(A beg, A end, B out)
The problem is that the arguments (the iterators from std::string
) are in the namespace std
and in this namespace, there is another algorithm called equal
which is a candidate due to argument dependent lookup (ADL). You need to explicitly qualify your algorithm:
std::cout << "a and b are " << ::equal(a.begin(), a.end(), b.begin()) << std::endl;
// ^^ here
Note that the C++ standard does not require the iterators to be a type in std
, but is allows it and you compiler/standard library decided to use this option.
这篇关于实现平等的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!