C++命名空间、标准库(std,全局命名空间)
背景
别人遇到的问题:
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.5\workspace\C++_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
C++命名空间、标准库(std,全局命名空间)的更多相关文章
- C++ 标准库 std::find 查找
参见:https://en.cppreference.com/w/cpp/algorithm/find 查找指定字符/数字等. #include <iostream> #include & ...
- C++ 标准库 std::npos 表示 size_t 的最大值
参见:https://en.cppreference.com/w/cpp/algorithm/find std::npos 表示 size_t 的最大值,常用于对查找结果成功与否的判断. #inclu ...
- C++ 标准库 std::remove
参见:https://zh.cppreference.com/w/cpp/algorithm/remove std::remove 不会改变输入vector / string 的长度.其过程,相当于去 ...
- 把《c++ primer》读薄(3-1 标准库string类型初探)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...
- c标准库 徐明远 背景基础
背景基础 1.c语言库用c语言编写 其他语言则不同 早期语言的库是用汇编语言编写的 不同的计算机体系结构有不同的汇编语言 所以在移植性方面差一点 而c语言可以编写出高度可移植性的代码 ...
- C++11 标准库也有坑(time-chrono)
恰巧今天调试程序遇到时间戳问题, 于是又搜了搜关于取时间戳,以及时间戳转字符串的问题, 因为 time() 只能取到秒(win和linux) 想试试看能不能找到 至少可以取到毫秒的, 于是, 就找 ...
- C++ Primer 学习笔记_6_标准库类型 -- 命名空间using与string类型
标准库类型(一) --命名空间using与string类型 引: 标准库类型是语言组成部分中更主要的哪些数据类型(如:数组.指针)的抽象! C++标准库定义的是高级的抽象数据类型: 1.高级:由 ...
- std 与标准库
1.命名空间std C++标准中引入命名空间的概念,是为了解决不同模块或者函数库中相同标识符冲突的问题.有了命名空间的概念,标识符就被限制在特定的范围(函数)内,不会引起命名冲突.最典型的例子就是st ...
- 未能在全局命名空间中找到类型或命名空间名称“Wuqi”
下载了AspNetPager控件用以进行分页操作,在项目中放入控件后,运行报错:未能在全局命名空间中找到类型或命名空间名称“Wuqi” . 解决办法:在项目下拉框“引用“中添加AspNetPager引 ...
随机推荐
- bzoj 2815
http://www.cnblogs.com/JS-Shining/archive/2013/01/12/2857429.html 题面 题解上写了用什么dominator tree,吓晕了,看了看, ...
- E20170523-hm
parse vt. 从语法上描述或分析(词句等); escape character エスケープ文字 转义符 arity [计] 数量; analyzevt. <美>分析; 分解; ...
- node.js在读取文件时中文乱码问题
断更很久了........从今天开始会努力的持续更博,积极学习. 言归正传.今天在写node.js的demo时发现一个bug.我在node中读取本地的text文件时,发现英文的内容可以被读取,但是中文 ...
- bzoj 4405: [wc2016]挑战NPC【带花树】
把每个筐子拆成3个,分别表示放0/1/2个,然后把这三个点两两连起来,每一个可以放在筐里的球都想这三个点连边. 这样可以发现,放0个球的时候,匹配数为1,放1个球的时候,匹配数为1,放2个球的时候,匹 ...
- 【转】Postman 使用方法详解
1.Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介 ...
- mysql 状态查询
select COUNT(case when info.State = '0' then State end ) as daichuliCount, COUNT(case when info.St ...
- 第二篇(那些JAVA程序BUG中的常见单词)
Cannot instantiate the type xxx 无法实例化类型xxx instantiate 实例化
- 采购发票检验MIRO差异科目设置
采购订单发票检验时,最终的金额可能跟采购订单的价格不一样,对于这部分差异,系统提供了后台配置科目的方式. 配置科目可通过OBYC,在BSX存货差异配置相关评估类型对应科目. 当库存商品少于采购订单数量 ...
- 笔记——malloc、free、不同数据类型操作、.pyc文件、python安装第三方包、验证一个网站的所有链接有效性
C — malloc( ) and free( ) C 语言中使用malloc( )函数申请的内存空间,为什么一定要使用free释放? **malloc()函数功能:是从堆区申请一段连续的空间,函数结 ...
- 《Windows核心编程系列》十一谈谈Windows线程池
Windows线程池 上一篇博文我们介绍了IO完成端口.得知IO完成端口可以非常智能的分派线程.但是IO完成端口仅对等待它的线程进行分派,创建和销毁线程的工作仍然需要我们自己来做. 我们自己也可以创建 ...