String Class
- Describe the string class and its member functions
"Use vector and string instead of arrays." Sutter, Alexandruscu (2005)
The C++ Standard Library defines the string class as the object-oriented alternative to null-terminated character strings of the C language. The member functions of this class that are specialized for direct operations on strings.
Class Definition
The string class is defined within the std namespace. The <string> header file contains the class definition.
Constructors
The constructors of the string class can initialize an object using:
- a C-style null-terminated string
- a C-style null-terminated substring
- another
stringobject - a substring of another
stringobject - a sequence of characters
That is,
string a = "Hello"; // a C-style string
string b("Good Bye", 4); // the 1st 4 characters of C-style string
string c = a; // another string object
string d(b, 5, 3); // "Bye" - a substring of a string object
string e(a.begin(), a.end()); // a sequence of characters
Member Functions and Operations
Member functions and operations of the string class include:
string& operator=(const string& s)- assign stringsto the current objectstring& operator+=(const string& s)- append stringsto the current objectstring& operator+=(const char* s)- append C-style stringsto the current objectstring& operator+=(char c)- append charactercto the current objectsize_t size() const- returns number of bytes stored in the current objectsize_t length() const- returns number of bytes stored in the current objectbool empty() const- returns whether the current object is emptychar& operator[](size_t i)- returns a reference to the character at indexi(unchecked)const char& operator[](size_t i) const- read the character at indexi(unchecked)at(size_t i) const- read the character at indexi(checked)char& front()- returns a modifiable reference to the first characterconst char& front() const- read the first characterchar& back()- returns a modifiable reference to the last characterconst char& back() const- read the last characterconst char* c_str() const- return the address of the C-style null-terminated string equivalentsize_t find_first_of(const string& str) const- returns the position of the first character that matches a character instr; if no match, returnsstring::npossize_t find_last_of(const string& str) const- returns the position of the last character that matches a character instr; if no match, returnsstring::nposstring substr(size_t pos, size_t len = npos)- returns a string that contains the portion of the current string starting at character positionposand spanninglencharacters (or to the end of the current string)int compare(const string& str) const- compares the current object tostrand returns0if the strings are equal, != 0 otherwisevoid clear()- erases the contents of the current object
npos is a class member constant with the greatest possible value for position within the string (unsigned(-1)).
Example
// String Class
// string.cpp
#include <string>
#include <iostream>
int main()
{
std::string str("Good ");
str += "day";
str += '!';
str[5] = 'D';
std::cout << str.length() << ' ' << str.c_str() << std::endl;
}
9 Good Day!
Helper Functions
Helper functions for the string class include:
std::istream& getline(std::istream&is, string& str, char delimiter)- extracts characters fromisuntil delimiter; stores the characters instrand discards the delimiterstring operator+(const string& lhs, const string& rhs)- concatenateslhstorhsand returns the concatenated stringistream& operator>>(istream& is, string& s)- extractssfromisand returns a reference toisostream& operator<<(ostream& os, const string& s)- insertssintoosand returns a reference toos
STL Related Notes
The string class is the default instantiation of the basic_string class template. The basic_string class template is similar to the vector class template. The string class provides iterators and can be used with the standard algorithms of the standard template library. Pointer/array equivalence does not hold: if s is an instance of the string class, then &s[0] is not the same as s.