C++ - 字符串是内置数据类型吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5388685/
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:03:14  来源:igfitidea点击:

C++ - Is string a built-in data type?

c++stringbuilt-in-types

提问by Simplicity

In C++, is stringa built-in data type?

C++,是string内置数据类型吗?

Thanks.

谢谢。

回答by David Rodríguez - dribeas

What is the definition of built-inthat you want to use? Is it built-inthe compiler toolset that you have yes, it should. Is it treated specially by the compiler? no, the compiler treats that type as any user defined type. Note that the same can probably be applied to many other languages for which most people will answer yes.

您要使用的内置的定义是什么?它是否内置于您的编译器工具集中是的,它应该。是不是被编译器特别处理了?,编译器将该类型视为任何用户定义的类型。请注意,这可能适用于大多数人会回答yes 的许多其他语言。

One of the focuses of the C++ committee is keeping the core language to a bare minimum, and provide as much functionality as possible in libraries. That has two intentions: the core language is more stable, libraries can be reimplemented, enhanced... without changing the compiler core. But more importantly, the fact that you do not need special compiler support to handle most of the standard library guarantees that the core language is expressive enough for most uses.

C++ 委员会的重点之一是将核心语言保持在最低限度,并在库中提供尽可能多的功能。这有两个意图:核心语言更稳定,库可以重新实现,增强......而不改变编译器核心。但更重要的是,您不需要特殊的编译器支持来处理大部分标准库,这一事实保证了核心语言对于大多数用途来说具有足够的表达能力。

Simpler said in negated form: if the language required special compiler support to implement std::stringthat would mean that users don't have enough power to express that or a similar concept in the core language.

简单地说,否定形式:如果语言需要特殊的编译器支持来实现std::string,那将意味着用户没有足够的能力在核心语言中表达那个或类似的概念。

回答by cHao

It's not a primitive -- that is, it's not "built in" the way that int, char, etc are. The closest built-in string-like type is char *or char[], which is the old C way of doing stringy stuff, but even that requires a bunch of library code in order to use productively.

这不是一个原始的-也就是说,它没有“内置”的方式intchar等都是。最接近的内置字符串类型是char *or char[],这是处理字符串的旧 C 方式,但即使这样也需要一堆库代码才能有效地使用。

Rather, std::stringis a part of the standard library that comes with nearly every modern C++ compiler in existence. You'll need to #include <string>(or include something else that includes it, but really you should include what your code refers to) in order to use it.

相反,std::string它是几乎所有现有的现代 C++ 编译器都附带的标准库的一部分。您需要#include <string>(或包含其他包含它的内容,但实际上您应该包含您的代码所指的内容)才能使用它。

回答by B?ови?

If you are talking about std::string then no.

如果你在谈论 std::string 那么不。

If you are talking about character array, I guess you can treat it as an array of a built in type.

如果您在谈论字符数组,我想您可以将其视为内置类型的数组。

回答by flight

No.

不。

Built-in or "primitive" types can be used to create string-life functionality with the built-in type char. This, along with utility functions were what was used in C. In C++, there is still this functionality but a more intuitive way of using strings was added.

内置或“原始”类型可用于通过内置类型创建字符串寿命功能char。这与实用程序函数一起在 C 中使用。在 C++ 中,仍然存在此功能,但添加了一种更直观的使用字符串的方式。

The stringclass is a part of the std namespace and is an instantiation of the basic_stringtemplate class. It is defined as

string班是std命名空间的一部分,是一个实例化basic_string模板类。它被定义为

typedef basic_string<char> string;

It is a class with the ability to dynamically resize as needed and has many member functions acting as utilities. It also uses operator overloading so it is more intuitive to use. However, this functionality also means it has an overhead in terms of speed.

它是一个能够根据需要动态调整大小的类,并且具有许多充当实用程序的成员函数。它还使用运算符重载,因此使用起来更直观。但是,此功能也意味着它在速度方面有开销。

回答by jho

Depends on what you mean by built-in, but probably not. std::stringis defined by the standard library (and thus the C++ standard) and is very universally supported by different compilers, but it is not a part of the core language like intor char.

取决于你所说的内置是什么意思,但可能不是。std::string由标准库(以及 C++ 标准)定义,并且受到不同编译器的普遍支持,但它不是核心语言的一部分,如intchar

回答by Bo Persson

It canbe built-in, but it doesn't have to be.

可以是内置的,但不是必须的。

The C++ standard library has a documented interface for its components. This can be realized either as library code or as compiler built-ins. The standard doesn't say how it should be implemented.

C++ 标准库为其组件提供了一个文档化的接口。这可以作为库代码或编译器内置程序来实现。该标准没有说明应该如何实施。

When you use #include <string>you have the std::string implementation available. This could be either because the compiler implements it directly, or because it links to some library code. We don't know for sure, unless we check each compiler.

当您使用时,您可以使用#include <string>std::string 实现。这可能是因为编译器直接实现了它,或者因为它链接到了一些库代码。我们不确定,除非我们检查每个编译器。

None of the known compilers have chosen to make it a built-in type, because they didn't have to. The performance of a pure library implementation was obviously good enough.

没有一个已知的编译器选择使它成为内置类型,因为他们没有必要这样做。纯库实现的性能显然已经足够好了。

回答by kill3rreaper

Definitely not. String is a class from standard library. char *, or char[]are built-in types, but char, int, float, double, void, bool without any additions (as pointers, arrays, sign or size modifiers - unsigned, long etc.) are fundamental types.

当然不。String 是标准库中的一个类。 char *char[]是内置类型,但没有任何添加(如指针、数组、符号或大小修饰符 - unsigned、long 等)的 char、int、float、double、void、bool 是基本类型。

回答by Kylo

No. It's part of standard library.

不,它是标准库的一部分。

回答by Adam Trhon

No, stringis a class.

不,字符串是一个类。

回答by Aidan

No. There are different imlementations (eg Microsoft Visual C++), but char* is the C++ way of representing strings.

不。有不同的实现(例如 Microsoft Visual C++),但 char* 是表示字符串的 C++ 方式。