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

  • C/++/#
  • Thread starter Mr Virtual
  • Start date
218
3

Main Question or Discussion Point

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

jtbell
Mentor
15,395
3,181
It's defined in <string>.

<string.h> is for C-style strings, not C++-style strings.
 
chroot
Staff Emeritus
Science Advisor
Gold Member
10,165
34
218
3
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:
jtbell
Mentor
15,395
3,181
#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.
 
218
3
Bingo! I found it in <bits/basic_string.h>
Thanks for the help and sorry for the trouble.

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

std::cout << ""

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

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

Replies
12
Views
1K
  • Last Post
Replies
10
Views
733
  • Last Post
Replies
2
Views
2K
Replies
3
Views
2K
Replies
2
Views
2K
  • Last Post
Replies
4
Views
819
  • Last Post
Replies
1
Views
1K
Replies
5
Views
9K
Top