零散知识笔记

📅 发布时间:2026/8/24 2:21:24
零散知识笔记
std 解析定义std是 C 标准库的命名空间namespace所有标准库的功能都放在里面。详细解释什么是命名空间命名空间就像一个容器用来组织代码避免名字冲突。生活类比 ┌─────────────────────────────────────────────────────────────────────┐ │ 想象一个小区里有多个叫张三的人 │ │ │ │ A栋的张三 → 要叫A栋的张三才能找到他 │ │ B栋的张三 → 要叫B栋的张三才能找到他 │ │ │ │ C 中也是一样 │ │ std::cout → std 里面的 cout │ │ mylib::cout → mylib 里面的 cout │ └─────────────────────────────────────────────────────────────────────┘std 里面有什么std:: ├── 容器 │ ├── std::vector // 动态数组 │ ├── std::list // 链表 │ ├── std::map // 键值对 │ └── std::string // 字符串 │ ├── 算法 │ ├── std::sort // 排序 │ ├── std::find // 查找 │ └── std::copy // 复制 │ ├── 输入输出 │ ├── std::cout // 标准输出 │ ├── std::cin // 标准输入 │ └── std::endl // 换行 │ └── 其他 ├── std::thread // 多线程 ├── std::unique_ptr // 智能指针 └── std::chrono // 时间代码示例不使用 std 命名空间#include iostream #include string #include vector int main() { // 每个标准库函数都要加 std:: std::string name 张三; std::vectorint numbers {1, 2, 3}; std::cout name std::endl; return 0; }使用 std 命名空间#include iostream #include string #include vector using namespace std; // 告诉编译器在 std 里找 int main() { // 不需要 std:: 前缀 string name 张三; vectorint numbers {1, 2, 3}; cout name endl; return 0; }只导入特定的名字推荐#include iostream #include string #include vector using std::cout; using std::endl; using std::string; int main() { string name 张三; // ✅ 可以使用 cout name endl; // ✅ 可以使用 // vector 仍然需要 std:: std::vectorint numbers {1, 2, 3}; // ✅ 明确 return 0; }为什么叫 std缩写全称中文stdStandard标准std Standard标准std 命名空间 Standard Library标准库面试高频题Q1std 是什么std是 C 标准库的命名空间所有标准库函数、类、对象都在这个命名空间中比如std::cout、std::string、std::vector。Q2为什么需要 std为了避免命名冲突。比如你写的sort函数和标准库的std::sort不会冲突。Q3std 和 STL 有什么区别概念说明std标准库的命名空间STL标准模板库容器、算法、迭代器标准库std 包含 STL 其他iostream、string 等标准库 STL容器、算法、迭代器 其他iostream、string、thread 等 所有标准库都在 std 命名空间中using namespace std;会把 std 命名空间中的所有名字导入到当前作用域可能导致命名冲突和代码可读性下降尤其是在大型项目中。问题详解1. 命名冲突最严重的问题#include iostream #include algorithm #include vector using namespace std; // ❌ 把整个 std 命名空间导入 int count 10; // 自定义变量 int main() { // 问题这个 count 是哪个 // 是自定义的 count 变量 // 还是 std::count 函数 count 20; // ❌ 编译器可能混淆 return 0; }更好的写法#include iostream #include algorithm #include vector int count 10; // 自定义变量 int main() { ::count 20; // 使用全局作用域 std::count 30; // 明确使用 std 命名空间 return 0; }2. 头文件中使用 using namespace std 污染全局// // myheader.h - ❌ 错误示例 // #ifndef MYHEADER_H #define MYHEADER_H #include string #include vector using namespace std; // ❌ 千万不要在头文件中这样写 class MyClass { string name; // 现在所有包含此头文件的文件都有了 std vectorint data; }; #endif // // 其他文件包含这个头文件后std 被污染了 // #include myheader.h int count 10; // 可能与 std::count 冲突❌更好的写法// // myheader.h - ✅ 正确示例 // #ifndef MYHEADER_H #define MYHEADER_H #include string #include vector class MyClass { std::string name; // ✅ 明确使用 std:: std::vectorint data; }; #endif3. 代码可读性下降#include iostream #include string #include vector #include algorithm using namespace std; // ❌ 不清晰 int main() { // 这些是 std 的还是自定义的 string s hello; vectorint v {1, 2, 3}; cout s endl; // 难以判断每个名字来自哪里 return 0; }更好的写法#include iostream #include string #include vector #include algorithm int main() { std::string s hello; // ✅ 明确来自 std std::vectorint v {1, 2, 3}; // ✅ 明确来自 std std::cout s std::endl; // ✅ 明确来自 std // 一眼就能看出每个名字的来源 return 0; }对比表对比项使用using namespace std不使用命名冲突❌ 容易发生✅ 避免代码可读性❌ 不清楚来源✅ 清晰可见头文件安全❌ 污染全局✅ 安全大项目维护❌ 困难✅ 容易编译速度略快少写 std::略慢多写 std::代码简洁度更简洁稍繁琐什么时候可以用场景一.cpp 文件中的有限范围// // ✅ 可以在 .cpp 文件中使用不推荐但可接受 // #include iostream #include string using namespace std; // 仅在 .cpp 中不影响其他人 int main() { string s hello; cout s endl; return 0; }场景二在函数内部使用#include iostream #include string void test() { using namespace std; // ✅ 仅影响当前函数 string s hello; cout s endl; } int main() { // 这里不受影响 return 0; }面试高频题Q1using namespace std有什么问题主要问题有三个命名冲突std中有count、find、sort等名字容易和自定义变量冲突头文件污染在头文件中使用会污染所有包含它的文件可读性下降不清楚名字来自std还是其他地方Q2在 .h 和 .cpp 中使用有什么区别头文件.h中绝对不要用会污染全局。源文件.cpp中可以用但建议在函数内部使用或者只使用特定的名字。Q3如果想用某个特定的 std 名字怎么办// 只导入特定的名字推荐 using std::string; using std::vector; using std::cout; using std::endl; // 这样只有这 4 个名字被导入 // 其他 std 名字不受影响最佳实践// // ✅ 推荐做法 // // 1. 头文件中明确使用 std:: #include string class MyClass { std::string name; // ✅ 明确 }; // 2. .cpp 文件中有选择的 using #include iostream #include string using std::string; // ✅ 只导入特定的 using std::cout; using std::endl; int main() { string s hello; // ✅ 可以使用 cout s endl; // ✅ 可以使用 return 0; } // // ❌ 不推荐 // // 头文件中 using namespace std; // ❌ 绝对不要 // 全局范围内 using namespace std; // ❌ 容易出问题vs按ctrld可以快速复制当前行到下一行