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

Discussion Overview

The discussion revolves around the definition and implementation of the 'string' data type in C++. Participants explore where to find its definition and clarify the distinction between C-style and C++-style strings.

Discussion Character

  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant suggests that 'string' is a class with certain functions and seeks guidance on its definition.
  • Another participant clarifies that 'string' is defined in the header , contrasting it with , which is for C-style strings.
  • A link to cppreference.com is provided for further information on the string class.
  • One participant shares a snippet of code from that includes various headers related to string functionality.
  • Another participant points out that using 'string' actually involves 'basic_string' and references a book by Stroustrup for more details.
  • A participant confirms finding the definition in and expresses gratitude for the assistance.
  • Code examples are shared to illustrate the usage of the 'string' data type, including namespace considerations.
  • One participant mentions that 'string' is a template in C++, providing a link for further reference.

Areas of Agreement / Disagreement

Participants generally agree on the location of the 'string' definition and its relationship to 'basic_string', but there are varying levels of detail and understanding regarding its implementation and usage.

Contextual Notes

Some participants reference specific versions of compilers and libraries, which may affect the availability and implementation of the 'string' data type.

Who May Find This Useful

This discussion may be useful for C++ programmers seeking to understand the definition and implementation of the 'string' data type, as well as those interested in the differences between C and C++ string handling.

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
5K
  • · 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
3K
  • · Replies 6 ·
Replies
6
Views
2K