In C++
, is string
a built-in data type?
Thanks.
In C++
, is string
a built-in data type?
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. 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.
built-in
to mean those compiler intrinsics (things not in the library). –
Anderton 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.
Rather, std::string
is 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.
If you are talking about std::string then no.
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
. 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.
The string
class is a part of the std namespace and is an instantiation of the basic_string
template class. 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
.
It can be 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.
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.
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.
No, string is a class.
string
class, which may not be possible under C++, so mentioning it as a library of methods will be more relevant in general sense. –
Mattheus char*
instead of string
object itself, and than methods are used separately, instead of accessing using object name, like we do in any OOP language eg; strobj.strrev();
. So as per standard OOP norms, string in C++ can't act as a class whose objects store values themselves and can access its methods. Though, in "C++ STL string", one can still use dot .
operator to access methods. –
Mattheus std::string
is a class. –
Amerind 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.
No. There are different imlementations (eg Microsoft Visual C++), but char* is the C++ way of representing strings.
char*
is the C way of representing strings, const char[]
is the type of string literals, std::string
is the standard way to represent strings. –
Centigram std::string
-type is standard and has a standard interface, so it /is/ the "C++ way" of representing strings, not char*
(which is also not quite right, if you mean string literals you should use const char*
). –
Vignette string
class is the C++ way. char*
is the C way. –
Epagoge © 2022 - 2024 — McMap. All rights reserved.