string (C++)
|
|
This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (October 2008) |
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 sourcesstring::~string(destructor) - Destructs the string and the contained charactersstring::operator=- Assigns values to the stringstring::assign- Assigns values to the stringstring::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 characterstring::front- Accesses the first characterstring::back- Accesses the last characterstring::data- Accesses the underlying array
- Iterators
- Capacity
string::empty- Checks whether the string is emptystring::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 stringstring::capacity- Returns the number of characters that can be held in currently allocated storagestring::shrink_to_fit(C++11) - Reduces memory usage by freeing unused memory
- Modifiers
string::clear- Clears the contentsstring::insert- Inserts charactersstring::erase- Erases charactersstring::push_back- Inserts characters to the endstring::append- Appends characters to the endstring::operator+=- Appends characters to the endstring::pop_back- Removes the last characterstring::resize- Changes the number of stored charactersstring::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
- ^ "C++ reference for
basic_string". Cppreference.com. http://cppreference.com/wiki/string/basic_string/. Retrieved 11 January 2011.