<string>
The essence of <string> is a dynamic array, essentially a std::vector<char>, that can conveniently operate text data.
#include <string>
#include <iostream>
int main() {
std::string s1 = "Hello";
std::string s2 = s2("World");
std::string s3 = s1 + " " + s2;
std::cout << s3 << std::endl;
}
Operations
std::string s = "abc";
s = "def";
s += "ghi";
s.append("jkl");
char c = s[0];
char c2 = s.at(0);
size_t len = s.size();
size_t cap = s.capacity();
bool empty = s.empty();
s.reserve(100);
s.clear();
- 4.Insert, Delete, Replace
s.insert(2, "AB");
s.erase(1, 3);
s.replace(0, 2, "XY");
size_t pos = s.find("XY");
std::string sub = s.substr(1, 3);
std::string a = "abc";
std::string b = "abd";
if (a == b) {
}
if (a < b) {
}
const char* cstr = s.c_str();
char * data = s.data();
std::string s = "hello";
for (size_t i = 0; i < s.size(); ++i) {
std::cout << s[i];
}
for (char c : s) {
std::cout << c;
}
for (auto it = s.begin; it != s.end(); ++it) {
std::cout << *it;
}