String (C++)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In the C++ programming language, the std::string class is a standard representation for a string of text. This class alleviates many of the problems introduced by C-style strings by putting the onus of memory ownership on the string class rather than on the programmer. The class provides some typical string operations like comparison, concatenation, find and replace, and a function for obtaining substrings. It can be constructed from a C-style string, and a C-style string can also be obtained from it.

Overview of functions[edit]

    • string::string(constructor) – Constructs the string from variety of sources
    • string::~string(destructor) – Destructs the string
    • string::operator= – Replace the string
    • string::assign – Replace the string
    • string::get_allocator – Returns the allocator used to allocate memory for the bytes
  • Byte access
    • string::at – Accesses specified byte with bounds checking.
    • string::operator[] – Accesses specified byte
    • string::front – Accesses the first byte
    • string::back – Accesses the last byte
    • string::data – Accesses the underlying array
  • Iterators
    • string::begin – Returns an iterator to the beginning of the string
    • string::end – Returns an iterator to the end of the string
    • string::rbegin – Returns a reverse iterator to the reverse beginning of the string
    • string::rend – Returns a reverse iterator to the reverse end of the string
  • Capacity
    • string::empty – Checks whether the string is empty
    • string::size – Returns the number of bytes in the string.
    • string::max_size – Returns the maximum possible number of bytes in the string.
    • string::reserve – Reserves storage in the string
    • string::capacity – Returns the current reservation
    • string::shrink_to_fit (C++11) – Reduces memory usage by freeing unused memory
  • Modifiers
    • string::clear – Clears the contents
    • string::insert – Inserts bytes or strings
    • string::erase – Deletes bytes
    • string::push_back – Appends a byte
    • string::append – Appends bytes or strings
    • string::operator+= – Appends
    • string::pop_back – Removes the last byte
    • string::resize – Changes the number of stored bytes
    • string::swap – Swaps the contents with another string
    • ...
  • Search
    • ...


#include <iostream>
#include <string>
using namespace std;
int main()
    string foo = "fighters";
    string bar = "stool";
    // "!=" compares string contents for inequality, even though they are different objects.
    if (foo != bar)
        cout << "The strings are different." << endl;
    // Prints "stool fighters" by creating a temporary object, which is automatically freed.
    cout << bar + " " + foo << endl;
    return 0;

Because a string may be stored by value, copying may take as long as O(n) (i.e., copying takes time proportional to the length of the string). It will also cause heap memory to be allocated, which is usually far more expensive than the copy. For that reason, string is generally passed by reference-to-const to avoid unnecessary copying:

void print_the_string(const string& str)
    cout << str;

To interoperate with C-interfaces, it is often necessary to obtain a null terminated string from a basic_string. The c_str() member function yields a pointer to the first element of an array whose elements correspond to the bytes in the original string and this array has a 0 at the offset of the length. If the null terminator is not needed, the data() method returns the pointer without any work needed to make sure the 0 is there. If the string is modified or its lifetime ends, the pointers returned by these methods become invalid.

Related classes[edit]

std::string is a typedef for a particular instantiation of the basic_string class template. Its definition is found in the <string> header and in the std namespace.[1]

typedef basic_string<char> string;

A std::basic_string can be made of any C++ object, even user-defined classes, similar to how a std::vector can be made of any class. They are also an STL container, and thus the functions in the STL Algorithm Library can be applied to the code units in strings, though these are of limited use as they rarely deal with sequences or patterns of items.

Thus the class std::string provides functionality for strings consisting of the C char[2] (not to be confused with actual "characters", which may consist of several of the units in the string, such as when UTF-8 encoding is used).

There is a similar class std::wstring, which consists of wchar_t, and is most often used to store UTF-16 text on Windows and UTF-32 on Unix. As the difference between these made writing portable code that shared data structures impossible, C++11 added two new classes: std::u16_string and std::u32_string (made up of the new types char16_t and char32_t), which are the given number of bits per code unit on all platforms.[3]


  1. ^ "C++ reference for basic_string". Retrieved 11 January 2011. 
  2. ^
  3. ^ "C++11 Paper N3336". Open Standards. Programming Language C++, Library Working Group. 13 Jan 2012. Retrieved 2 Nov 2013.