character judge and caption convert
isalpha
islower
isupper
isalnum //alpha and num
isblank //space and '\t'
isspace //space 't' '\r' '\n'
#include <cctype>
tolower
toupper
CPP class string
string is a specified container, so there are many same methods of container is also valid for class string.
1. Constructor
- default (1)
string();
- copy (2)
string (const string& str);
- substring (3)
string (const string& str, size_t pos, size_t len = npos);
- from c-string (4)
string (const char* s);
- from buffer (5)
string (const char* s, size_t n);
- fill (6)
string (size_t n, char c);
- range (7)
template <class InputIterator> string (InputIterator first, InputIterator last);
2. Random Access Iterators
cont.begin()
cont.end()
cont.rbegin()
3. Element Access
cont.at(index)
return a reference to the requested element; cont.at(index) = value;
cout << cont.at(index)
cont[index]
cont.front()
return a reference to the first element
cont.back()
return a reference to the last element
4. Capacity
cont.empty()
return true if the container is empty
cont.size() eqto s.length()
returns the number of elements in the container
5. Modifiers
- operator +=
s += s1
//string, c-string, char - append
- string (1)
string& append (const string& str);
- substring (2)
string& append (const string& str, size_t subpos, size_t sublen);
- c-string (3)
string& append (const char* s);
- buffer (4)
string& append (const char* s, size_t n);
- fill (5)
string& append (size_t n, char c);
- range (6)
template <class InputIterator> string& append (InputIterator first, InputIterator last);
[first,last)
- string (1)
cont.clear()
erases all elements- insert
- string (1)
string& insert (size_t pos, const string& str);
- substring (2)
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
- c-string (3)
string& insert (size_t pos, const char* s);
- buffer (4)
string& insert (size_t pos, const char* s, size_t n);
- fill (5)
string& insert (size_t pos, size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);
- single character (6)
iterator insert (const_iterator p, char c);
- range (7)
template <class InputIterator>
iterator insert (iterator p, InputIterator first, InputIterator last);
- initializer list (8)
string& insert (const_iterator p, initializer_list<char> il);
- string (1)
cont.push_back(T& val)
Adds a new element “val” at the end of the vectorvoid pop_back()
Removes the last element in the vector, effectively reducing the container size by one. No return.void swap (string& str);
Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ.
6. string operation
- find: Searches the string for the first occurrence of the sequence specified by its arguments.
- string (1)
size_t find (const string& str, size_t pos = 0) const;
- c-string (2)
size_t find (const char* s, size_t pos = 0) const;
- buffer (3)
size_t find (const char* s, size_t pos, size_t n) const;
- character (4)
size_t find (char c, size_t pos = 0) const;
when pos is specified, the search ignores any characters before pos - rfind : Searches the string for the last occurrence of the sequence specified by its arguments.
- string (1)
- substr
string substr (size_t pos = 0, size_t len = npos) const;
- caps transform
#include <algorithm>
transform(s.begin(), s.end(), s.begin(), ::tolower);
7. non-member function
getline(is, str);
Extracts characters fromis
and stores them into str until the newline character, ‘\n’ is found- Relational operators
8. other useful functions in header “string”
- convert from stings
stoi(); stoll(); stod()
- convert to strings
to_string()
return a string from a numerical value