Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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++ STL算法函数名解析_C++_Stl_Namespaces - Fatal编程技术网

C++ STL算法函数名解析

C++ STL算法函数名解析,c++,stl,namespaces,C++,Stl,Namespaces,我希望在示例中,bellow编译器将无法编译代码,因为 不知道什么是“find()”,它在算法头的std命名空间中定义 但是,此代码在RHEL 5.3和gcc 4.1.2上编译 我错过了什么 #include <string> #include <algorithm> int main() { std::string s; find(s.begin(), s.end(), 'a'); // should not compile } #包括 #

我希望在示例中,bellow编译器将无法编译代码,因为 不知道什么是“find()”,它在算法头的std命名空间中定义

但是,此代码在RHEL 5.3和gcc 4.1.2上编译

我错过了什么

#include <string>    
#include <algorithm>

int main()
{
    std::string s;
    find(s.begin(), s.end(), 'a');  // should not compile
}
#包括
#包括
int main()
{
std::字符串s;
find(s.begin(),s.end(),'a');//不应编译
}

由于依赖于参数的查找,因此此操作有效。在参数类型的命名空间中搜索函数模板。在本例中,参数是
std::string::iterator
,因此在命名空间
std

@Nick:note中搜索函数。参见下面的答案。参见和。+1-ADL在某些上下文中开始变得更有意义:例如,扩展boost::hash_值可以使用ADL.Aka Koenig lookup来完成。dimba:如果你想向自己证明这一点,你可以看到
::find
在全局名称空间中找不到匹配项,并且给定
字符a[1]
find(&a[0],&a[0],'a')
将找不到
std::find()模板
…只是为了详细说明ADL存在的原因,“主”ADL的理由是让操作员自然工作。给定两个
std::string
对象
s1
s2
,您希望能够将它们与
s1+s2
连接起来。没有ADL,您必须编写
std::operator+(s1,s2)
。但是,对于其他情况,它已经证明是一种非常有用的机制。下面展示了另一个关于如何使用ADL自定义标准库的好例子。@jalf:这个例子真的正确吗?就我所见(今晚不远,要到凌晨4点才能停止玩愚蠢的游戏),一个全局
::操作符+(const std::string&,const std::string&)
无论如何都可以匹配。因此,它更多地是关于找到正确的
find()
,尽管在与调用代码关联的中间名称空间中存在其他不相关的
find()
s。。。。