Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

C++ STL string

Created by : Mr Dk.

2018 / 09 / 17 20:38

Nanjing, Jiangsu, China


Template

typedef basic_string<char> string;
#include <string>
using namespace std; // using std::string;

Constructor

string();                                                 // Empty constructor (default)
string(const string& str);                                // Copy constructor
string(const string& str, size_t pos, size_t len = npos); // Substring contructor
  • pos : Beginning of character position
  • len : Length that wants to be copied
  • (string is too short) | ( len == string::npos ) -> copy until the end of string
string (const char* s);           // From C-String
string (const char* s, size_t n); // From C-String, copy first n characters
string (size_t n, char c); // Fill the string with n copies of character c
// Range constructor
template<class InputIterator> string(InputIterator first, InputIterator last);

Member Constants

static const size_t npos = -1;
  • The greatest possible value for an element of type size_t
  • When used as the value for a length parameter: means "Until the end of the string"
  • When used as a return value: indicating no matches

Iterators

string::iterator begin_iter = str.begin();            // 指向字符串开头的迭代器
string::iterator end_iter = str.end();                // 指向字符串结尾的下一个位置的迭代器
string::reverse_iterator r_begin_iter = str.rbegin(); // 返回指向字符串结尾的反向迭代器
string::reverse_iterator r_end_iter = str.rend();     // 返回指向字符串开头的前一个位置的反向迭代器

Operators Overload

Operator =: return a COPY of object.

string& operator= (const string& str);  // str = str1;
string& operator= (const char* s);      // str = "Hello world"
string& operator= (char c);             // str = '.'

Operator +: return a newly constructed object after concatenation.

string operator+ (const string& lhs, const string& rhs);  // str = str1 + str2;
string operator+ (const string& lhs, const char* rhs);    // str = str1 + "Hello";
string operator+ (const char* lhs, const string& rhs);    // str = "Hello" + str2;
string operator+ (const string& lhs, char rhs);           // str = str1 + '.';
string operator+ (char lhs, const string& rhs);           // str = '.' + str2;

Operator +=: return the same object after appending value.

string& operator+= (const string& str);  // str += str1;
string& operator+= (const char* s);      // str += "Hello world";
string& operator+= (char c);             // str += '.'

Operator []: return a reference to the character at position in the string. (string::at())

  • if (position == string length): \0
  • C++11
    • front(): access the first character
    • back(): access the last character
    • If the string is empty: undefined behavior
char& operator[] (size_t pos);

const char& operator[] (size_t pos) const;
      char& at(size_t pos);
const char& at(size_t pos) const;
      char& front();
const char& front() const;
      char& back();
const char& back() const;

Operator >>: for input.

istream& operator>> (istream& is, string& str);

Operator <<: for output

ostream& operator<< (ostream& os, const string& str);

Capacity

Return the length of the string:

size_t size() const;
size_t length() const;

Return the maximum length of the string can reach:

size_t max_size() const;

Return size of allocated storage:

size_t capacity() const;

Resizes the string to a length of n characters

  • If c is specified, the new elements are initialized as copies of c; Otherwise, they are value-initialized characters (null characters)
  • If n is smaller than current string; the current value is shortened to its first n character
void resize(size_t n);
void resize(size_t n, char c);

Request a change in capacity:

  • If n is greater than the current string capacity, the function causes the container to increase its capacity to n characters (or greater)
  • Exception
    • GUARANTEE: If an exception is thrown, there are NO changes in the string
    • If n > max_size: length_error exception
    • If the function needs to allocate storage and fails: bad_alloc exception
void reserve(size_t n = 0);

Erases the contents of the string: empty string.

void clear();

Returns whether the string is empty:

bool empty() const;

Modifications

Assigns a new value to the string, replacing its current contents. Exception:

  • s is not long enough | range is not valid: undefined behavior
  • subpos > string length: out_of_range exception
  • Resulting string length > max_size: length_error exception
  • The function needs to allocate storage and fails: bad_alloc exception
string& assign(const string& str);                               // String
string& assign(const string& str, size_t subpos, size_t sublen); // Substring
string& assign(const char* s);                                   // C-String
string& assign(const char* s, size_t n);                         // Buffer
string& assign(size_t n, char c);                                // Fill
template <class InputIterator> string& assign(InputIterator first, InputIterator last); // Range

Appending additional characters at the end of its current value. Exception:

  • s is not long enough | range is not valid: undefined behavior
  • subpos > string length: out_of_range exception
  • Resulting string length > max_size: length_error exception
  • The function needs to allocate storage and fails: bad_alloc exception
string& append(const string& str);                               // String
string& append(const string& str, size_t subpos, size_t sublen); // Substring
string& append(const char* s);                                   // C-String
string& append(const char* s, size_t n);                         // Buffer
string& append(size_t n, char c);                                // Fill
template <class InputIterator> string& append(InputIterator first, InputIterator last); // Range

Insert into the string right before the character indicated by pos or p. Exception:

  • s is not long enough | range is not valid: undefined behavior
  • subpos > str's length: out_of_range exception
  • Resulting string length > max_size: length_error exception
  • The function needs to allocate storage and fails: bad_alloc exception
string& insert(size_t pos, const string& str);                                      // String
string& insert(size_t pos, const string& str, size_t subpos, size_t sublen = npos); // Substring
string& insert(size_t pos, const char* s);                                          // C-String
string& insert(size_t pos, const char* s, size_t n);                                // Buffer
string& insert(size_t pos, size_t n, char c);                                       // Fill
iterator insert(const_iterator p, size_t n, char c);                                // Fill
iterator insert(const_iterator p, char c);                                          // Single Character
template <class InputIterator> iterator insert(iterator p, InputIterator first, InputIterator last); // Range

Erase characters from string. Exception:

  • If pos > string length: out_of_range exception
  • If range is invalid: undefined behavior
string& erase(size_t pos = 0, size_t len = npos); // Sequence
iterator erase(iterator p);                       // Character
iterator erase(iterator first, iterator last);    // Range

Replace portion of string, specify a range in old string by:

  • pos & len
  • A pair of iterators

Or specify a new string to replace into the specific range.

Exception:

  • If s is not long enough | range is not valid: undefined behavior
  • pos or subpos is greater than string's length: out_of_range exception
  • If the resulting string length > max_size: length_error exception
  • If the function needs to allocate storage and fails: bad_alloc exception
// String
string& replace(size_t pos, size_t len, const string& str);
string& replace(const_iterator i1, const_iterator i2, const string& str);
// Substring
string& replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen = npos);
// C-String
string& replace(size_t pos, size_t len, const char* s);
string& replace(const_iterator i1, const_iterator i2, const char* s);
// Buffer
string& replace(size_t pos, size_t len, const char* s, size_t n);
string& replace(const_iterator i1, const_iterator i2, const char* s, size_t n);
// Fill
string& replace(size_t pos, size_t len, size_t n, char c);
string& replace(const_iterator i1, const_iterator i2, size_t n, char c);
// Range
template <class InputIterator> string& replace(const_iterator i1, const_iterator i2, InputIterator first, InputIterator last);

Swap string values:

void swap(string& str);            // Member function -> str1.swap(str2);
void swap(string& x, string& y);   // Not a member function -> swap(str1, str2);

Append character to the end of the string, increasing its length by one. Exception:

  • Resulting string length > max_size: length_error exception
  • The function needs to allocate storage and fails: bad_alloc exception
void push_back(char c);

Delete the last character of the string, reducing its length by one. Exception:

  • If the string is empty: undefined behavior
void pop_back();

String Operations

  • Get C String with \0 in the end
const char* c_str() const noexcept;
const char* data() const noexcept;

Copy sequence of characters from string (string -> char[]), returning the number of characters copied to the array. Exception:

  • If s is not long enough: undefined behavior
  • If pos > string's length: out_of_range exception
size_t copy(char* s, size_t len, size_t pos = 0) const;

Compare strings - Compared string & Comparing string.

ValueReason
0Compare equal.
<0Either the value of the first character that does not match is lower in the compared string, or all characters match but compared string is shorter.
>0Either the value of the first character that does not match is greater in the compared string, or all characters match but compared string is longer.

Exception:

  • If s is not long enough: undefined behavior
  • If pos or subpos > string's length: out_of_range exception
// String
int compare(const string& str) const noexcept;
// Substrings
int compare(size_t pos, size_t len, const string& str) const;
int compare(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
// C-String
int compare(const char* s) const;
int compare(size_t pos, size_t len, const char* s) const;
// Buffer
int compare(size_t pos, size_t len, const char* s, size_t n) const;

Generating substring. Exception

  • If pos > string's length: out_of_range exception
  • If the function needs to allocate storage and fails: bad_alloc exception
string substr(size_t pos = 0, size_t len = npos) const;

Find content in string - matching the entire string. Exception:

  • If s is not long enough: undefined behavior
  • pos is used to specify the first character to start searching
  • n is used to specify the length of s
  • If there is no match, return string::npos
// Find the first occurrence
size_t find(const string& str, size_t pos = 0) const noexcept;     // String
size_t find(const char* s, size_t pos = 0) const;                  // C-String
size_t find(const char* s, size_t pos, size_type n) const;         // Buffer
size_t find(char c, size_t pos = 0) const noexcept;                // Character

// Find the last occurrence
size_t rfind(const string& str, size_t pos = npos) const noexcept; // String
size_t rfind(const char* s, size_t pos = npos) const;              // C-String
size_t rfind(const char* s, size_t pos, size_t n) const;           // Buffer
size_t rfind(char c, size_t pos = npos) const noexcept;            // Character

Find character in string - matching any of the characters specified in arguments. Exception:

  • If s is not long enough: undefined behavior
  • pos is used to specify the first character to start searching
  • n is used to specify the length of s
  • If there is no match, return string::npos
// The first character matched
size_t find_first_of(const string& str, size_t pos = 0) const noexcept;  // String
size_t find_first_of(const char* s, size_t pos = 0) const;               // C-String
size_t find_first_of(const char* s, size_t pos, size_t n) const;         // Buffer
size_t find_first_of(char c, size_t pos = 0) const noexcept;             // Character

// The first character absent
size_t find_first_not_of(const string& str, size_t pos = 0) const noexcept; // String
size_t find_first_not_of(const char* s, size_t pos = 0) const;              // C-String
size_t find_first_not_of(const char* s, size_t pos, size_t n) const;        // Buffer
size_t find_first_not_of(char c, size_t pos = 0) const noexcept;            // Character

// The last character matched
size_t find_last_of(const string& str, size_t pos = npos) const noexcept; // String
size_t find_last_of(const char* s, size_t pos = npos) const;              // C-String
size_t find_last_of(const char* s, size_t pos, size_t n) const;           // Buffer
size_t find_last_of(char c, size_t pos = npos) const noexcept;            // Character

// The last character absent
size_t find_last_not_of(const string& str, size_t pos = npos) const noexcept; // String
size_t find_last_not_of(const char* s, size_t pos = npos) const;              // C-String
size_t find_last_not_of(const char* s, size_t pos, size_t n) const;           // Buffer
size_t find_last_not_of(char c, size_t pos = npos) const noexcept;            // Character

Input

作为基本数据类型,可以直接使用 cin & >>。另外,还可以使用 getline():

  • default delimitation character: \n
istream& getline(istream&  is, string& str, char delim);
istream& getline(istream&& is, string& str, char delim);
istream& getline(istream&  is, string& str);
istream& getline(istream&& is, string& str);

Conversion

将数据类型转换为字符串:

#include <string>
using std::to_string

string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

将字符串转换为数据类型:

#include <string>
using std::stoi;
using std::stol;
using std::stoul;
using std::stoll;
using std::stoull;
using std::stof;
using std::stod;
using std::stold;

int stoi (const string&  str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
long stol (const string&  str, size_t* idx = 0, int base = 10);
long stol (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long stoul (const string&  str, size_t* idx = 0, int base = 10);
unsigned long stoul (const wstring& str, size_t* idx = 0, int base = 10);
long long stoll (const string&  str, size_t* idx = 0, int base = 10);
long long stoll (const wstring& str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const string&  str, size_t* idx = 0, int base = 10);
unsigned long long stoull (const wstring& str, size_t* idx = 0, int base = 10);
float stof (const string&  str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
double stod (const string&  str, size_t* idx = 0);
double stod (const wstring& str, size_t* idx = 0);
long double stold (const string&  str, size_t* idx = 0);
long double stold (const wstring& str, size_t* idx = 0);

Summary

Useful & important.

Edit this page on GitHub
Prev
C++ STL set
Next
C++ STL unordered_map