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

  • C/++/#
  • Thread starter Mr Virtual
  • Start date
  • #1
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:

Answers and Replies

  • #2
jtbell
Mentor
15,631
3,677
It's defined in <string>.

<string.h> is for C-style strings, not C++-style strings.
 
  • #4
218
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
jtbell
Mentor
15,631
3,677
#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
218
4
Bingo! I found it in <bits/basic_string.h>
Thanks for the help and sorry for the trouble.

Mr V
 
  • #7
15
0
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
15
0
if you dont use the namespace make sure with like:

std::cout << ""

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

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

Replies
12
Views
1K
  • Last Post
Replies
10
Views
833
  • Last Post
Replies
2
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
  • Last Post
Replies
4
Views
910
  • Last Post
Replies
1
Views
2K
Replies
5
Views
10K
  • Last Post
Replies
5
Views
11K
  • Last Post
Replies
14
Views
3K
Top