C++: How is the data type 'string' defined?

In summary, the 'string' data type in C++ is actually the 'basic_string' template defined in <bits/basic_string.h>. This can be found by looking into <string> or in Stroustrup's "The C++ Programming Language." The 'string' template is used to create strings in C++ and can be used with or without the 'std' namespace.
  • #1
Mr Virtual
218
4
I read somewhere that 'string' is basically a class having certain functions. I looked into string.h and _mingw.h but could not find its defintion. Can anyone guide me as to where to look, or give me an idea of how it may be defined?

Warm regards
Mr V
 
Last edited:
Technology news on Phys.org
  • #2
It's defined in <string>.

<string.h> is for C-style strings, not C++-style strings.
 
  • #4
I looked in <string> but all I found there was as shown below:

#ifndef _GLIBCXX_STRING
#define _GLIBCXX_STRING 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <bits/stringfwd.h>
#include <bits/char_traits.h>
#include <memory> // For allocator.
#include <bits/type_traits.h>
#include <iosfwd> // For operators >>, <<, and getline decls.
#include <bits/stl_iterator.h>
#include <bits/stl_function.h> // For less
#include <bits/basic_string.h>

#ifndef _GLIBCXX_EXPORT_TEMPLATE
# include <algorithm> // for find_if
# include <bits/basic_string.tcc>
#endif

#endif /* _GLIBCXX_STRING */


I am using Dev C++ version 4.9.9.2
 
Last edited:
  • #5
Mr Virtual said:
#include <bits/basic_string.h>

Try looking there. When you use the 'string' data type, you're actually using 'basic_string'. I think Stroustrup's "The C++ Programming Language" has some details on the connection.
 
  • #6
Bingo! I found it in <bits/basic_string.h>
Thanks for the help and sorry for the trouble.

Mr V
 
  • #7
What you want to do is here is an example:

#include <string>
#include <iostream>

using namespace std;

int main(int argc, char **argv){

string mystr = "myString";
cout << "My String is: " << mystr << "\n";
}
 
  • #8
if you don't use the namespace make sure with like:

std::cout << ""

you do:
std::string mystr = "";
 

1. What is the data type 'string' in C++?

The data type 'string' in C++ is a sequence of characters that is used to store and manipulate text or character data.

2. How is the 'string' data type defined in C++?

The 'string' data type is defined in C++ as a class that provides various functions and operations for handling string data.

3. Can the 'string' data type hold other types of data besides characters?

Yes, the 'string' data type in C++ can hold other types of data such as integers, floating-point numbers, and even other strings.

4. How is memory allocated for the 'string' data type in C++?

Memory for the 'string' data type in C++ is allocated dynamically, which means that the memory is allocated at runtime and can be resized as needed.

5. Are there any limitations to the length of a 'string' in C++?

Yes, there are limitations to the length of a 'string' in C++. The maximum length of a 'string' may vary depending on the compiler and the system, but it is typically around 2 billion characters.

Similar threads

  • Programming and Computer Science
2
Replies
47
Views
2K
  • Programming and Computer Science
Replies
1
Views
172
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
Back
Top