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

  • Context: C/C++ 
  • Thread starter Thread starter Mr Virtual
  • Start date Start date
  • Tags Tags
    Data String Type
Click For Summary
SUMMARY

The C++ data type 'string' is defined in the header file , which utilizes the underlying class 'basic_string'. The discussion clarifies that is intended for C-style strings and not applicable for C++. Users should reference for the actual implementation details. Additionally, Bjarne Stroustrup's "The C++ Programming Language" provides further insights into the relationship between 'string' and 'basic_string'.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with C++ standard library headers
  • Knowledge of template classes in C++
  • Basic understanding of C-style vs. C++-style strings
NEXT STEPS
  • Explore the implementation of in C++
  • Learn about C++ string manipulation functions
  • Study template classes in C++ for better understanding of 'string'
  • Read Bjarne Stroustrup's "The C++ Programming Language" for in-depth knowledge
USEFUL FOR

C++ developers, software engineers, and students seeking to deepen their understanding of string data types and their implementations in C++.

Mr Virtual
Messages
218
Reaction score
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 definition. 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
It's defined in <string>.

<string.h> is for C-style strings, not C++-style strings.
 
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:
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.
 
Bingo! I found it in <bits/basic_string.h>
Thanks for the help and sorry for the trouble.

Mr V
 
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";
count << "My String is: " << mystr << "\n";
}
 
if you don't use the namespace make sure with like:

std::count << ""

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

Similar threads

  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 47 ·
2
Replies
47
Views
4K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K