当前位置: 首页 > news >正文

C++ 学习与 CLion 使用:(二)using namespace std 语句详解,以及 std 空间的标识符罗列

using namespace std

1)作用

在 C++ 中,using namespace std; 的作用是引入标准命名空间 std 中的所有标识符,使得在代码中可以直接使用 std 中的名称(如 coutvectorstring 等,类似 python 中的关键字),而无需显式添加 std:: 前缀。

  • 如果不使用 using namespace std;,每次调用标准库功能时都必须显式加上 std:: 前缀:

    #include <iostream>int main() {std::cout << "Hello, World!" << std::endl;  // 必须写 std::std::string s = "C++";return 0;
    }
    
  • 而使用 using namespace std; 后,可以省略 std::

    #include <iostream>
    using namespace std;  // 引入 std 命名空间int main() {cout << "Hello, World!" << endl;  // 直接使用string s = "C++";return 0;
    }
    

注意:虽然这样可以省事,但是如果自定义的标识符(如变量、函数)与 std 中的名称相同,会导致冲突。也会降低代码可读性。

注意:在头文件中绝对不要使用 using namespace std;

2)用法

  • 局部使用(推荐)

    仅在函数内部或 .cpp 文件中局部引入,避免在头文件中使用:

    #include <iostream>void printHello() {using namespace std;  // 仅在此作用域内有效cout << "Hello!" << endl;
    }
    
  • 显式引入特定名称

    只引入需要的名称,而非整个 std

    #include <iostream>
    using std::cout;  // 仅引入 cout
    using std::endl;  // 仅引入 endlint main() {cout << "Hello!" << endl;  // 可以直接使用// string s = "C++";       // 错误!未引入 std::stringreturn 0;
    }
    
  • 完全避免(最安全)

    直接使用 std:: 前缀,明确来源:

    #include <iostream>int main() {std::cout << "Hello!" << std::endl;  // 始终显式指定std::string s = "C++";return 0;
    }
    

3)std 命名空间核心内容表格

分类子分类关键成员
输入/输出 (I/O)标准流cin, cout, cerr, clog
文件流ifstream, ofstream, fstream
字符串处理字符串类string, wstring, u16string, u32string
字符串操作getline, stoi, to_string, stod
容器顺序容器vector, list, deque, array, forward_list
关联容器(有序)set, map, multiset, multimap
无序关联容器(哈希表)unordered_set, unordered_map, unordered_multiset, unordered_multimap
算法排序与搜索sort, find, binary_search, lower_bound, upper_bound
修改操作copy, reverse, unique, fill, transform
数值计算accumulate, inner_product, iota, partial_sum
迭代器通用工具begin, end, next, prev, distance, advance
实用工具时间处理chrono::seconds, chrono::milliseconds, system_clock
随机数生成mt19937, uniform_int_distribution, shuffle
智能指针unique_ptr, shared_ptr, weak_ptr, make_shared, make_unique
类型特性与转换move, forward, is_integer, enable_if, conditional
异常处理异常类exception, runtime_error, logic_error, invalid_argument
多线程线程管理thread, mutex, lock_guard, unique_lock, condition_variable
函数式编程函数对象plus, minus, less, equal_to, greater
绑定与可调用对象bind, function, mem_fn, not_fn
数学函数通用数学abs, sqrt, sin, cos, floor, ceil, pow, log
C 兼容性C 标准库封装printf, scanf, fopen, malloc, free(通过 <cstdio> 等头文件提供)

http://www.dtcms.com/a/328167.html

相关文章:

  • 消防安全预警系统助力安全生产
  • 【工作笔记】win11系统docker desktop配置国内mirror不生效解决方案汇总整理
  • `SHOW PROCESSLIST;` 返回列详解(含义 + 单位)
  • django celery 动态添加定时任务后不生效问题
  • 【SDR课堂第35讲】通用软件无线电平台USRP7440- RFSOC NCO性能测试(一)
  • android 换肤框架详解3-自动换肤原理梳理
  • JDK 9~17 新特性及升级建议
  • 【154页PPT】某大型再生资源集团管控企业数字化转型SAP解决方案(附下载方式)
  • 麒麟信安“操作系统+云”双驱动,推进某市公安局智慧警务建设
  • 云部署 MCP 服务计费
  • Java 包
  • 飞算JavaAI:Java智能开发工具的技术解析、应用实践
  • 燕山大学计算机网络实验(2025最新)
  • Python科学计算与可视化领域工具TVTK、Mayavi、Mlab、Traits(附视频教程)
  • 【AI】Pycharm中要注意Python程序文件的位置
  • 【C#】正则表达式
  • Lyapunov与SAC算法的数学结构对比:从二次漂移到TD损失
  • 【KO】Android Framework
  • 串口超时参数深度解析:ReadTotalTimeoutMultiplier、ReadIntervalTimeout等
  • 熟悉并使用Spring框架 - 注解篇
  • JavaWeb从入门到精通!第二天!(Servlet)
  • Jenkins + SonarQube 从原理到实战三:SonarQube 打通 Windows AD(LDAP)认证与踩坑记录
  • 基于多模态大模型的个性化学习路径生成系统研究
  • 循环神经网络(RNN)全面解析
  • 运维学习Day22——Anisible自动化与基本使用
  • SpringBoot面试宝典
  • MySQL User表入门教程
  • Spyglass CDC rule
  • NLP—词向量转换评论学习项目分析
  • 28.分类算法:让机器学会分类