简体   繁体   English

std 代表什么?

[英]What does std stands for?

I understand that if there is not using namespace std , and you want to write a cout , you need to have a std::cout .我知道如果没有using namespace std ,并且你想写一个cout ,你需要有一个std::cout What does the std represents? std代表什么? Why is std widely used ( std::vector , std::cout , std::cin )?为什么std被广泛使用( std::vectorstd::coutstd::cin )?

std stands for "standard". std 代表“标准”。

The reason why so much standard stuff goes in the std namespace is simple: Before namespaces, different code written by different people would often use the same name and cause a conflict. std命名空间中有这么多标准的东西的原因很简单:在命名空间之前,不同人编写的不同代码经常会使用相同的名称并导致冲突。 For example, my drink dispenser program from 1994 might have a class ofstream which is an orange fanta stream.例如,我从 1994 年开始的饮料分配器程序可能有一个流类,它是橙色ofstream流。 When a new version of C++ came along and added ofstream which was an output file stream, my program wouldn't compile any more, or it'd crash.当新版本的 C++ 出现并添加了作为输出文件流的ofstream时,我的程序将不再编译,或者它会崩溃。

Okay, orange-fanta-stream is silly, but major operating systems do have C functions called open , close , and index .好吧,orange-fanta-stream 很傻,但主要操作系统确实有 C 函数,称为opencloseindex I'm sure many people have tried to make global variables called open , and then their programs have crashed.我敢肯定,很多人都试图将全局变量称为open ,然后他们的程序就崩溃了。

In C++, all the new C++ standard stuff is inside std:: , so as long I don't call something in my program std , they can add new stuff inside std:: and it definitely won't cause this problem.在 C++ 中,所有新的 C++ 标准的东西都在std::中,所以只要我不在我的程序std中调用某些东西,他们就可以在std::中添加新的东西,它绝对不会导致这个问题。 Unfortunately all the stuff that C++ inherits from C is outside std:: , so you still can't make a global variable called open (on Linux), but at least it's a start.不幸的是,C++ 从 C 继承的所有东西都在std::之外,所以你仍然不能创建一个名为open的全局变量(在 Linux 上),但至少这是一个开始。

It's a namespace reserved for the C++ standard library.它是为 C++ 标准库保留的命名空间。

A good choice historically: any token starting with std is reserved in C. So that meant that early C++ compilers would not break existing C code.历史上一个不错的选择:任何以std开头的标记都保留在 C 中。这意味着早期的 C++ 编译器不会破坏现有的 C 代码。 Of course the languages have diverged since then.当然,从那时起,语言就出现了分歧。 Etymologically, std is an abbreviation for standard.在词源上, std是标准的缩写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM