Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the implementation for std::string

I am looking for the code for the c++ string class. What header is it implemented in?

like image 443
Alan Avatar asked Jul 03 '10 04:07

Alan


People also ask

What library is std::string in?

The string class is part of the C++ standard library.

How are C++ strings implemented?

std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.

Where is std::string defined?

std::string is a typedef for a particular instantiation of the std::basic_string template class. Its definition is found in the <string> header: using string = std::basic_string<char>; Thus string provides basic_string functionality for strings having elements of type char.

Where is C++ string stored?

@user1145902: They are stored in memory like in an array, but that memory is not allocated in the stack (or wherever the string object is), but rather in a dynamically allocated buffer.


1 Answers

There is no single implementation for std::string. But you can find your particular implementation in the <string> header.

On my system it can be found here:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.0/include/g++-v4/bits/basic_string.h and /usr/lib/gcc/x86_64-pc-linux-gnu/4.5.0/include/g++-v4/bits/basic_string.tcc

On a Debian-based system:

~$ locate basic_string.tcc
/usr/include/c++/4.5/bits/basic_string.tcc
/usr/include/c++/4.6/bits/basic_string.tcc
~$ locate basic_string.h
/usr/include/c++/4.5/bits/basic_string.h
/usr/include/c++/4.6/bits/basic_string.h
~$ 

Generally, you are going to be looking for the basic_string template, since std::string is just a specialization of that.

like image 142
Evan Teran Avatar answered Sep 24 '22 12:09

Evan Teran