简体   繁体   English

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

[英]C++ - Is string a built-in data type?

In C++ , is string a built-in data type?C++中, string是内置数据类型吗?

Thanks.谢谢。

What is the definition of built-in that you want to use?您要使用的内置定义是什么? Is it built-in the 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 .请注意,这可能适用于大多数人会回答“”的许多其他语言。

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. C++ 委员会的重点之一是将核心语言保持在最低限度,并在库中提供尽可能多的功能。 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.但更重要的是,您不需要特殊的编译器支持来处理大多数标准库这一事实保证了核心语言对大多数用途具有足够的表现力。

Simpler said in negated form: if the language required special compiler support to implement std::string that would mean that users don't have enough power to express that or a similar concept in the core language. Simpler 以否定形式表示:如果语言需要特殊的编译器支持来实现std::string ,这将意味着用户没有足够的能力在核心语言中表达那个或类似的概念。

It's not a primitive -- that is, it's not "built in" the way that int , char , etc are.它不是原语——也就是说,它不像intchar等那样“内置”。 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.最接近的内置类字符串类型是char *char[] ,这是旧的 C 处理字符串的方式,但即使那样也需要一堆库代码才能有效地使用。

Rather, std::string is a part of the standard library that comes with nearly every modern C++ compiler in existence.相反, std::string是标准库的一部分,几乎所有现存的现代 C++ 编译器都附带了该标准库。 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.您需要#include <string> (或包括其他包含它的东西,但实际上您应该包括您的代码所指的内容)才能使用它。

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.如果您谈论的是字符数组,我想您可以将其视为内置类型的数组。

No.不。

Built-in or "primitive" types can be used to create string-life functionality with the built-in type char .内置或“原始”类型可用于通过内置类型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.这与实用函数一起在 C 中使用。在 C++ 中,仍然有此功能,但添加了一种更直观的使用字符串的方式。

The string class is a part of the std namespace and is an instantiation of the basic_string template class. string类是 std 命名空间的一部分,是basic_string模板类的实例。 It is defined as它被定义为

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.然而,这个功能也意味着它在速度方面有开销。

Depends on what you mean by built-in, but probably not.取决于你所说的内置是什么意思,但可能不是。 std::string is 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 int or char . std::string由标准库(因此也是 C++ 标准)定义,并且得到不同编译器的普遍支持,但它不像intchar那样是核心语言的一部分。

It can be built-in, but it doesn't have to be.可以是内置的,但不是必须的。

The C++ standard library has a documented interface for its components. C++ 标准库为其组件提供了文档化的接口。 This can be realized either as library code or as compiler built-ins.这可以作为库代码或编译器内置函数来实现。 The standard doesn't say how it should be implemented.该标准没有说明应该如何实施。

When you use #include <string> you have the std::string implementation available.当您使用#include <string>时,您可以使用 std::string 实现。 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.我们不确定,除非我们检查每个编译器。

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.纯库实现的性能显然已经足够好了。

Definitely not.当然不。 String is a class from standard library. String 是标准库中的一个类。 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. char *char[]是内置类型,但 char、int、float、double、void、bool 没有任何添加(如指针、数组、符号或大小修饰符 - unsigned、long 等)是基本类型。

No. It's part of standard library.不,它是标准库的一部分。

No, string is a class.不,字符串是一个类。

In C++ , is string a built-in data type?C++string是内置数据类型吗?

Thanks.谢谢。

No. There are different imlementations (eg Microsoft Visual C++), but char* is the C++ way of representing strings.不。有不同的实现(例如 Microsoft Visual C++),但 char* 是 C++ 表示字符串的方式。

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

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