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.

Contents

[edit] Overview of functions

    • string::string(constructor) - Constructs the string from variety of sources
    • string::~string(destructor) - Destructs the string and the contained characters
    • string::operator= - Assigns values to the string
    • string::assign - Assigns values to the string
    • string::get_allocator - Returns the allocator used to allocate memory for the characters
  • Character access
    • string::at - Accesses specified character with bounds checking.
    • string::operator[] - Accesses specified character
    • string::front - Accesses the first character
    • string::back - Accesses the last character
    • 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 characters in the string.
    • string::max_size - Returns the maximum possible number of characters in the string.
    • string::reserve - Reserves storage in the string
    • string::capacity - Returns the number of characters that can be held in currently allocated storage
    • string::shrink_to_fit (C++11) - Reduces memory usage by freeing unused memory
  • Modifiers
    • string::clear - Clears the contents
    • string::insert - Inserts characters
    • string::erase - Erases characters
    • string::push_back - Inserts characters to the end
    • string::append - Appends characters to the end
    • string::operator+= - Appends characters to the end
    • string::pop_back - Removes the last character
    • string::resize - Changes the number of stored characters
    • string::swap - Swaps the contents with another string
    • ...
  • Search
    • ...

[edit] Usage

#include <iostream>
#include <string>
 
int main()
{
    std::string foo = "fighters";
    std::string bar = "stool";
 
    // "!=" compares string contents for inequality, even though they are different objects.
    if (foo != bar)
    {
        std::cout << "The strings are different." << std::endl;
    }
 
    // Prints "stool fighters" by creating a temporary object, which is automatically freed.
    std::cout << bar + " " + foo << std::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 std::string& str)
{
    std::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.

[edit] Related classes

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;

which is equivalent to

basic_string<char, char_traits<char>, allocator<char> >

Thus the class std::string provides functionality for strings consisting of the C char[1] (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. You can also make "strings" of any other object, even user-defined classes, similar to how you can make a std::vector of any class.

[edit] References

Personal tools
Namespaces

Variants
Actions
Navigation
Interaction
Toolbox
Print/export