Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 实现equal()和find()_C++_Argument Dependent Lookup - Fatal编程技术网

C++ 实现equal()和find()

C++ 实现equal()和find(),c++,argument-dependent-lookup,C++,Argument Dependent Lookup,在下面的代码中,我必须限定equal()调用(否则我会得到“对重载函数的不明确调用”),但可以调用unqualifiedfind()。有什么区别 #include <iterator> #include <vector> #include <iostream> using std::vector; using std::cout; using std::endl; // Test whether the elements in two ranges are

在下面的代码中,我必须限定
equal()
调用(否则我会得到“对重载函数的不明确调用”),但可以调用unqualified
find()
。有什么区别

#include <iterator>
#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

// Test whether the elements in two ranges are equal.
// Compares the elements in the range [b,e) with those in the range
// beginning at d, and returns true if all of the elements in both ranges match.
template <class InIter1, class InIter2>
bool equal(InIter1 b, InIter1 e, InIter2 d)
{
    cout << "My equal()" << endl;
    while (b != e)
        if ((*b++) != (*d++))
            return false;
    return true;
}

// Returns an iterator to the first element in the range [b,e)
// that compares equal to t. If no such element is found, the function returns last.
template <class InIter, class T>
InIter find( InIter b, InIter e, const T& t )
{
    cout << "My find()" << endl;
    while (b != e) {
        if ( *b == t )
            return b;
        b++;
    }

    return e;
}

/* "Accelerated C++", ex. 8.2 */
int main()
{
    static const int arr[] = {8, 7, 15, 21, 30};
    vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]) );

    cout << "equal() result: " << ::equal(vec.begin(), vec.end(), vec.rbegin()) << endl;

    vector<int>::iterator iter = find(vec.begin(), vec.end(), 21);
    if (iter == vec.end())
        cout << "did not find()" << endl;
    else
        cout << "find() result: " << *iter << endl;

    return 0;
}
#包括
#包括
#包括
使用std::vector;
使用std::cout;
使用std::endl;
//测试两个范围内的元素是否相等。
//将[b,e]范围内的元素与范围内的元素进行比较
//从d开始,如果两个范围中的所有元素都匹配,则返回true。
模板
布尔相等(初始值为b,初始值为e,初始值为d)
{

cout您的标准库实现似乎从另一个文件中引入了
std::equal
,而该文件也没有引入
std::find
(可能用于vector的比较运算符)。如果您包含
,它们都将被引入,并且两种情况下都会出现歧义。

假设您使用std::vector
而不是
使用命名空间std;
它看起来像是ADL yes。如果是这样,那么它根本找不到相关的
std::find
。我没有检查您包含的标题是什么UnANTETED提供,但最突出的一点是,在C++中,它们不保证不包括其他标准的LIB头。@ C欢呼和Hth.-阿尔夫,你似乎是对的(包括不同之处)。如果您想添加答案,我会接受。谢谢。Benjamin写的答案基本上与答案相同。:)因此答案是:find和equal之间没有区别,这很令人欣慰。它只是包含了标题,只能提供一个函数。