背景
别人遇到的问题:
C++ 全局变量不明确与 using namespace std 冲突
我遇到的问题与他相似,函数调用冲突
using namespace std;
class compareFun {
public:
bool operator()(const string& string1, const string& string2) {
string temp1;
string temp2;
temp1.resize(string1.size());
temp2.resize(string2.size());
transform(string1.begin(),string1.end(),temp1.begin(),::tolower);
transform(string2.begin(),string2.end(),temp2.begin(),::tolower);
return temp1<temp2;
}
};
上述代码transform(string1.begin(),string1.end(),temp1.begin(),::tolower);
中,tolower前不加::
会编译不同过。
不加::,此时有两个函数实现的来源,localefwd.h和ctype.h
加了::
后,查看该函数,出现在ctype.h中,经查
ctype.h是C标准函数库中的头文件,定义了一批C语言字符分类函数(C character classification functions),用于测试字符是否属于特定的字符类别,如字母字符、控制字符等等。既支持单字节(Byte)字符,也支持宽字符。
但是,我突然想起一个问题,localefwd.h和ctype.h我都没有导入,为什么我可以直接使用?
下面是完整的头文件
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
经过gcc -M gets.cpp
查看头文件的依赖情况,发现好多,原因应该是编译器的自动推导功能:
PS D:CLion 2018.2.5workspaceC++_day01> gcc -M gets.cpp
gets.o: gets.cpp
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/iostream
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/c++config.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/os_defines.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/cpu_defines.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ostream
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ios
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/iosfwd
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stringfwd.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/memoryfwd.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/postypes.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/cwchar
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/wchar.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/crtdefs.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw_mac.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw_secapi.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/vadefs.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sdks/_mingw_directx.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sdks/_mingw_ddk.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw_print_push.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw_off_t.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw_stat64.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/swprintf.inl
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sec_api/wchar_s.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/_mingw_print_pop.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/exception
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/atomic_lockfree_defines.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/char_traits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_algobase.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/functexcept.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/exception_defines.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/cpp_type_traits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ext/type_traits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ext/numeric_traits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_pair.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/move.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/concept_check.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_iterator_base_types.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_iterator_base_funcs.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/debug/debug.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_iterator.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/ptr_traits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/predefined_ops.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/localefwd.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/c++locale.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/clocale
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/locale.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/stdio.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sec_api/stdio_s.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/cctype
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/ctype.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/ios_base.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ext/atomicity.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/gthr.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/gthr-default.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/pthread.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/stddef.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/stddef.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/errno.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sys/types.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/process.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed/limits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include-fixed/syslimits.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/limits.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/signal.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/pthread_signal.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sys/timeb.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sec_api/sys/timeb_s.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/pthread_compat.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/pthread_unistd.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/atomic_word.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/locale_classes.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/string
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/allocator.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/c++allocator.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ext/new_allocator.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/new
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/ostream_insert.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/cxxabi_forced.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_function.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/backward/binders.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/range_access.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/basic_string.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/basic_string.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/locale_classes.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/streambuf
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/streambuf.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/basic_ios.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/locale_facets.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/cwctype
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/wctype.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/ctype_base.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/streambuf_iterator.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/x86_64-w64-mingw32/bits/ctype_inline.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/locale_facets.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/basic_ios.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/ostream.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/istream
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/istream.tcc
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/algorithm
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/utility
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_relops.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_algo.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/cstdlib
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/stdlib.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/sec_api/stdlib_s.h
D:/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include/malloc.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/algorithmfwd.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_heap.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_tempbuf.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_construct.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/ext/alloc_traits.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/set
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_tree.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_set.h
D:/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/bits/stl_multiset.h
C/C++标准库
那什么是c标准库??
C标准库
C标准库也称为ISO C库,是用于完成诸如输入/输出处理、字符串处理、内存管理、数学计算和许多其他操作系统服务等任务的宏、类型和函数的集合。它是在C标准中(例如C11标准)中定义的。其内容分布在不同的头文件中,比如上面我所提到的math.h。C++标准库
和C标准库的概念类似,但仅针对C
++。C++标准库是一组C++模板类,它提供了通用的编程数据结构和函数,如链表、堆、数组、算法、迭代器和任何其他你可以想到的C++组件。C ++标准库也包含了C标准库,并在C++标准中进行了定义(例如C++ 11标准)。
C/C++标准库主要包含3部分: STL、IO流及本地化、C的函数库。标准库不是STL,STL是标准模板库,是标准库的一个子集。它是一个可复用的组件库,其中包含了很多实用的算法和数据结构。stl是一个泛型思维的集中体现。
出自:什么是 C 和 C ++ 标准库?
-std(标准库)和STL(标准模板库)的关系
命名空间
std是什么?
#include<iostream>
int main()
{
std::cout<<"我喜欢C++";//输出一句话
std::cout<<std::endl;//换行
return 0;
}
std::是个名称空间标示符,C++标准库中的函数或者对象都是在命名空间std中定义的,所以我们要使用标准函数库中的函数或对象都要使用std来限定。
对象cout是标准函数库所提供的对象,而标准库在名字空间中被指定为std,所以在使用cout的时候要加上std::
。这样编译器就会明白我们调用的cout是名字空间std中的cout。一般来说,std都是要调用C++标准库时,要写上std; 使用非标准库文件iostream.h,不用写。
全局命名空间:
全局作用域中定义的名字(即在所有类、函数及命名空间之外定义的名字)也就是定义在全局命名空间(global
namespace)中。全局命名空间以隐式的方式声明,并且在所有程序中都存在。全局作用域中定义的名字被隐式地添加到全局命名空间中。
标准命名空间std为了解决C++标准库中的标识符与程序中的全局标识符之间以及不同库中的标识符之间的同名冲突,应该将不同库的标识符在不同的命名空间中定义(或声明)。标准C++库的所有的标识符都是在一个名为std的命名空间中定义的,或者说标准头文件(如iostream)中函数、类、对象和类模板是在命名空间> std中定义的。std是standard(标准)的缩写,表示这是存放标准库的有关内容的命名空间,含义请楚,不必死记。
这样,在程序中用到C++标准库时,需要使用std作为限定。
对std和全局命名空间和疑惑
c++标准库 及 命名空间std