C++ 中的 std::string 与字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5499189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:17:22  来源:igfitidea点击:

std::string vs string in c++

c++stringstd

提问by OVERTONE

Possible Duplicates:
Why is 'using namespace std;' considered a bad practice in C++?
Using std Namespace

可能的重复:
为什么是“使用命名空间 std;” 在 C++ 中被认为是一种不好的做法?
使用 std 命名空间

Ive been hovering around a bunch of different forums and i seem to see this pop up every time and again. Its a very much beginner question.

我一直在一堆不同的论坛上徘徊,我似乎一次又一次地看到这个弹出窗口。这是一个非常初学者的问题。

I usually define a program with

我通常定义一个程序

#include<string>
using namespace std;

string x;

I see a bunch of code samples out there who define a string as

我看到一堆代码示例将字符串定义为

std::string.

What is the purpose of this? is it good practice or have some functionality?

这样做的目的是什么?这是好的做法还是有一些功能?

采纳答案by ThiefMaster

As the other answer already stated, using std::is necessary unless you import either the whole stdnamespace or std::string(see below).

正如另一个答案已经指出的那样,std::除非您导入整个std命名空间或std::string(见下文),否则使用是必要的。

In my opinion it's nicer to use std::stringinstead of stringas it explicitly shows that it's a std::stringand not some other string implementation.

在我看来,使用它更好,std::string而不是string因为它明确表明它是一个std::string而不是其他一些字符串实现。

If you prefer to write just stringthough, I'd suggest you to use using std::string;instead of using namespace std;to only import the things into the global namespace that you actually require.

如果您更喜欢直接编写string,我建议您使用using std::string;而不是using namespace std;仅将内容导入到您实际需要的全局命名空间中。

回答by templatetypedef

The full name of stringis std::stringbecause it resides in namespace std, the namespace in which all of the C++ standard library functions, classes, and objects reside.

的全名stringstd::string因为它驻留在namespace std命名空间中,所有 C++ 标准库函数、类和对象都驻留在该命名空间中。

In your code, you've explicitly added the line using namespace std;, which lets you use anything from the standard namespace without using the std::prefix. Thus you can refer to std::string(the real name of the string type) using the shorthand stringsince the compiler knows to look in namespace stdfor it.

在您的代码中,您明确添加了 line using namespace std;,它允许您使用标准命名空间中的任何内容而无需使用std::前缀。因此,您可以std::string使用速记来引用(字符串类型的真实名称),string因为编译器知道要查找namespace std它。

There is no functionality difference between stringand std::stringbecause they're the same type. That said, there are times where you would prefer std::stringover string. For example, in a header file, it is generally not considered a good idea to put the line using namespace std;(or to use any namespace, for that matter) because it can cause names in files that include that header to become ambiguous. In this setup, you would just #include <string>in the header, then use std::stringto refer to the string type. Similarly, if there ever was any ambiguity between std::stringand some other stringtype, using the name std::stringwould remove the ambiguity.

之间没有功能差异stringstd::string因为它们是相同的类型。这就是说,有次在那里你宁愿std::stringstring。例如,在头文件中,放置行using namespace std;(或使用任何命名空间,就此而言)通常不被认为是一个好主意,因为它可能导致包含该头的文件中的名称变得不明确。在此设置中,您只需#include <string>在标题中,然后使用std::string来引用字符串类型。同样,如果std::string和其他string类型之间有任何歧义,使用名称std::string将消除歧义。

Whether or not you include the line using namespace std;at all is a somewhat contested topic and many programmers are strongly for or strongly against it. I suggest using whatever you're comfortable with and making sure to adopt whatever coding conventions are used when working on a large project.

是否包含该行using namespace std;是一个颇有争议的话题,许多程序员强烈支持或强烈反对它。我建议使用任何你喜欢的东西,并确保在处理大型项目时采用任何编码约定。

回答by Assambar

As ssteinbergsaid it is the same. But only syntactically.

正如ssteinberg所说,这是一样的。但只是在语法上。

From my experience, it is more useful to either use the full name

根据我的经验,使用全名更有用

std::string 

wherever you need it, or add an explicit

在您需要的任何地方,或添加明确的

using std::string;

in the beginning of your source file.

在源文件的开头。

The advantage is that in this way the dependencies of your source file are more observable. If you add a

这样做的好处是,你的源文件的依赖关系更容易被观察到。如果你添加一个

using namespace std;

it means that your code may depend on anything within that namespace.

这意味着您的代码可能依赖于该命名空间中的任何内容。

回答by MByD

If they are not usingthe namespace std, then they have to specify what namespace string belong to by using std::string

如果不是usingnamespace std,那么他们必须使用指定什么名称空间字符串属于std::string

回答by unexpectedvalue

If you have using namespace std;before your code it is the same.

如果你using namespace std;在你的代码之前有它是一样的。