hex_refugeeeee 2021-09-30 16:53 采纳率: 50%
浏览 14

在essential c++第3章中根据一个例题的template泛型程序

《essential c++》 p75


template<typename IteratorType, typename elemType>
IteratorType
find(IteratorType first, IteratorType last, const elemType &value)
{
    for (; first != last; ++first)
        if (value == *first)
            return first;
    return last;
}

int main()
{
    const int asize = 8;
    int ia[asize] = { 1,1,2,3,5,8,13,21 };

    vector<int> ivec(ia, ia + asize);
    list<int> ilist(ia, ia + asize);

    int *pia = find(ia, ia + asize, 1024);
    if (pia != ia + asize)
        cout << "0000" << endl;

    //vector<int>::iterator it;
    //it = find(ivec.begin(), ivec.end(), 1024);
}

在main中使用find总会报多个重载函数冲突。
我感觉应该没有问题,不知道怎么就是编译通不过。

img

  • 写回答

1条回答 默认 最新

  • 偷窃月亮的贼 2021-09-30 16:57
    关注

    std 中也有个函数叫做 find() ,编译器不知道是哪一个了。

    解决方案就是去掉开头的

    using namespace std;
    

    然后更改你的输入输出语句:

    • cout 改为 std::cout
    • cin 改为 std::cin
    • endl 改为 std::cin
    评论

报告相同问题?

问题事件

  • 创建了问题 9月30日