3. Character and String

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)
  • 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);
  • cont.push_back(T& val) Adds a new element “val” at the end of the vector
  • void 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.
  • 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 from is 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值