I am looking for the code for the c++ string class. What header is it implemented in?
4 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.

- 87,561
- 32
- 179
- 238
-
Thank you. It appears only the prototype of the method I was looking for: find() is in there "size_type find(const _CharT* __s, size_type __pos, size_type __n) const;" – Alan Jul 03 '10 at 14:26
-
ahhh sorry. I've found it in basic_string.tcc. Thanks! – Alan Jul 03 '10 at 14:28
-
1gcc implementation is here: https://gcc.gnu.org/onlinedocs/gcc-4.8.1/libstdc++/api/a01053_source.htm – Yiannis Mpourkelis Dec 04 '15 at 11:07
As you might expect,
<string>
which will most likely be located in whatever include
directory your compiler has as its base.

- 507,862
- 82
- 626
- 550
-
2you are of course right, but keep in mind that (IIRC) `
` doesn't actually have to be an actual file. I never seen an implementation where it wasn't though... – Evan Teran Jul 03 '10 at 04:19 -
It isn't a file on VMS; header files are contained in 'text libraries', whatever they are. Why, I've no idea. – Brian Hooper Jul 03 '10 at 08:58
It's in <string>
. It's a header file distributed with your compiler. It may include other (private) header files - a lot of the implementation for Visual Studio is in a file named "xstring".

- 11,775
- 1
- 35
- 44
A similar question with the answer for Visual Studio: https://stackoverflow.com/a/17205896/5520058
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\
The version and location of Visual Studio can be changed. This is the default installation path where the sources can be found.
Visual studio versions:
- 2005 -> 8
- 2008 -> 9
- 2010 -> 10
- 2012 -> 11
- 2013 -> 12
- 2015 -> 14