C++ - Is string a built-in data type?
Solution 1
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.
Solution 2
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.
Solution 3
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.
Solution 4
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
.
Solution 5
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.
Simplicity
Updated on July 09, 2022Comments
-
Simplicity almost 2 years
In
C++
, isstring
a built-in data type?Thanks.
-
David Rodríguez - dribeas over 13 yearsNot really, not precise.
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. -
filmor over 13 yearsDifferent implementations? In fact the
std::string
-type is standard and has a standard interface, so it /is/ the "C++ way" of representing strings, notchar*
(which is also not quite right, if you mean string literals you should useconst char*
). -
Kushal over 13 yearsRather than class, string is library that provides string manipulation methods instead of object to store strings.
-
dandan78 over 13 yearsThis is incorrect. The
string
class is the C++ way.char*
is the C way. -
Adam Trhon over 13 years@Kush: Maybe it is simplified, but even in C++ Standard (open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf) they speak about a string class (Chapter 21, Strings Library)
-
Kushal over 13 years@Dadam: agreed with the term, but those who are "only" familiar with the term "class" might wonder if they can create object of
string
class, which may not be possible under C++, so mentioning it as a library of methods will be more relevant in general sense. -
Adam Trhon over 13 years@Kush: I didn't know that. Can you give me some simple example?
-
Kushal over 13 years@Dadam: this page shows how string object gets created but the memory to store string is allocated using
char*
instead ofstring
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. -
Steve Jessop over 13 years@Kush: standard OOP terminology is irrelevant to what Dadam is saying - the C++ standard defines what "class" means in the context of C++, and
std::string
is a class. -
edA-qa mort-ora-y over 13 yearsThe standard makes use of the phrase
built-in
to mean those compiler intrinsics (things not in the library). -
Lightness Races in Orbit about 8 years"char* is the C++ way of representing strings" wtf