Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Apr 17, 2008 #1
    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: Apr 17, 2008
  2. jcsd
  3. Apr 17, 2008 #2


    User Avatar

    Staff: Mentor

    It's defined in <string>.

    <string.h> is for C-style strings, not C++-style strings.
  4. Apr 17, 2008 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

  5. Apr 18, 2008 #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>

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

    #endif /* _GLIBCXX_STRING */

    I am using Dev C++ version
    Last edited: Apr 18, 2008
  6. Apr 18, 2008 #5


    User Avatar

    Staff: Mentor

    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.
  7. Apr 18, 2008 #6
    Bingo! I found it in <bits/basic_string.h>
    Thanks for the help and sorry for the trouble.

    Mr V
  8. Apr 24, 2008 #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";
  9. May 10, 2008 #8
    if you dont use the namespace make sure with like:

    std::cout << ""

    you do:
    std::string mystr = "";
  10. May 13, 2008 #9


    User Avatar

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook