string中的常用构造方法
string();
构造一个空字符串。string(const char* s);
用C风格字符串构造string类的对象string(size_t n,char c);
构造一个含有n个字符c的字符串。string(const string& s);
拷贝构造方法。
//构造方法
void Teststring1() {
string s;
string s1("hello world");
cout << s1 << endl;
const char* p = "hello world";
string s2(p);
cout << s2 << endl;
string s3(s1);
cout << s3 << endl;
string s4(10, 'A');
cout << s4 << endl;
}
遍历的常用方法
- 下标访问操作符
void Teststring2() {
string s1("hello");
for (size_t i = 0; i < s1.size(); i++) {
cout << s1[i];
}
cout << endl;
}
- 迭代器
void Teststring3() {
//正向迭代器
string s1("hello");
string::iterator it = s1.begin();
while (it != s1.end()) {
cout << *it;
it++;
}
cout << endl;
//反向迭代器
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *rit;
++rit;
}
cout << endl;
}
- 范围for
void Teststring4() {
//范围for
string s1("hello");
for (auto e : s1) {
cout << e;
}
cout << endl;
}
string中对容量操作的常用方法
size_t size() const
返回字符串的有效长度.size_t length() const
也是返回字符串的有效字符长度size_t capacity()const
返回的是对象总容量的大小bool empt()const
检测字符串是否为空void clear()
清空字符串中的有效字符void resize(size_t n , char c)
将字符串中的有效字符改成n个,多余的用字符c填充,也可以不传默认用\0填充。void reverse(size_t n = 0)
将字符串容量大小调整到n
void Teststring5() {
string s1("hello");
cout << s1.size() << endl;
cout << s1.capacity() << endl;
if (s1.empty()) {
cout << "空"<< endl;
}
else {
cout << "不空"<< endl;
}
s1.clear();
//size被清空 capacity没变
cout << s1.size() << endl;
cout << s1.capacity() << endl;
}
void Teststring6() {
string s1("hello world");
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.resize(20, '!');
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.resize(60, '!');
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
}
容量不够时会自动扩容,不同编译器内部扩容规则不太一样,容量够不会改变capacity的大小。
void Teststring7() {
string s1("hello");
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.reserve(10);
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.reserve(20);
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
s1.reserve(100);
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
//15
s1.reserve(15);
cout << s1 << endl;
cout << s1.size() << endl;
cout << s1.capacity() << endl;
cout << sizeof(string) << endl;
}
接收的参数大于内部的容量时会扩容,如果是缩减时,当参数大于15时,内部容量不会改变,当参数小于等于15时,容量缩小为15。(vs2019)
string改变字符串内容的常用方法
-
void push_back (char c);
在末尾插入字符c -
string& append (const string& str);
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
在字符串末尾追加字符或者字符串 -
operator+=
在字符串末尾追加字符或者字符串 -
size_t find (const string& str, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const
从pos
从pos位置向后查找字符或者字符串,返回在原字符串中的位置。 -
size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (char c, size_t pos = npos) const
从pos位置向前查找字符或者字符串,返回在原字符串中的位置。 -
string substr (size_t pos = 0, size_t len = npos) const;
从pos位置开始截取n个字符返回截取到的字符串。 -
string& erase (size_t pos = 0, size_t len = npos);
从pos位置开始删除多长的元素。
void Teststring8() {
string s1("hello");
s1 += 'c';
cout << s1 << endl;
s1 += "world";
cout << s1 << endl;
s1.append(3,'!');
cout << s1 << endl;
string s2("world");
s2 = s2.append(s2, 1, 3);
cout << s2 << endl;
string s4("hello");
}
//insert
void Teststring9() {
string s("world");
s.insert(0, "hello");
cout << s << endl;
s.insert(4, 1, ' ');
cout << s << endl;
}
//erase
void Teststring10() {
string s("hello");
s.erase(0,1);
cout << s << endl;
s.erase(s.begin());
cout << s << endl;
s.erase(s.begin(), s.end());
cout << s << endl;
}
void Teststring11()
{
string s("12345");
//c风格字符串
int value = atoi(s.c_str());
cout << value << endl;
}
void Teststring12() {
string s("lol.exe");
string suffix = s.substr(s.find('.')+1);
cout << suffix << endl;
string s1("F:\\Game\\lol.exe");
size_t pos1 = s1.rfind('\\')+1;
size_t pos2 = s1.rfind('.')-1;
string filename = s1.substr(pos1, pos2-pos1);
cout << filename << endl;
}
void Teststring13() {
string s1("abcdefgh");
size_t n = s1.find("efg", 0);
cout << n << endl;
}