目录
常用函数一览
//默认成员函数
string();
string(const char* s);
string(size_t n, char c);
string(const string& str);
//与容量有关的函数
size_t size() const;
size_t capacity() const;
void reserve(size_t n);
void resize(size_t n, char c);
void reserve(size_t n = 0);
bool empty() const;
void clear();
//与访问及遍历有关的操作
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
// 与修改有关的操作
string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);
string& append(const char* s);
void push_back(char c);
string& insert(size_t pos, const char* s);
string& insert(size_t pos, size_t n, char c);
string& erase(size_t pos = 0, size_t len = npos);
//与string相关的其他常用函数
const char* c_str() const;
size_t find(char c, size_t pos = 0) const;
size_t rfind(char c, size_t pos = npos) const;
string substr(size_t pos = 0, size_t len = npos) const;
//常用非成员函数
istream& operator>> (istream& is, string& str);
ostream& operator<< (ostream& os, const string& str);
istream& getline(istream& is, string& str);
默认成员函数
1)string();//构造空的string类对象,即空的字符串
2)string(const char* s);//用C语言形式的字符串来构造string类对象
3)string(size_t n, char c);//用n个字符c来构造string类对象
4)string(const string& str); //用已有对象去构造新的对象
5)string& operator= (const string& str);//赋值重载
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1;
string s2("hello world!");
string s3(10, 'x');
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
string s4(s2);
cout << "s4:" << s4 << endl;
s1 = s2;
cout << "s1:" << s1 << endl;
}
int main()
{
test();
return 0;
}
运行结果:
与容量有关的函数
part 1
1)size_t size() const;
2)size_t capacity() const;
3)bool empty() const;
4)void clear();
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1("hello world!");
cout << "size:" << s1.size() << endl;
cout << "capacity:" << s1.capacity() << endl;
cout << "empty:" << s1.empty() << endl;
s1.clear();
cout << "After clear check empty:" << s1.empty() << endl;
}
int main()
{
test();
return 0;
}
运行结果:
part 2
1)void resize(size_t n);
2)void resize(size_t n, char c);
resize(size_t n)和resize(size_t n, char c)都是将字符串的有效个数改变到n个,不同的是,当使用 std::string::resize(size_t n)
来增加字符串的大小时,新增加的字符默认是用空字符(null character,即 \0
)来填充,resize(size_t n, char c)则用字符c来填充多出来的元素空间。
当n小于原来字符串的个数时,相当于删除操作,字符串将保留n个字符。
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1("hello world!");
s1.resize(20);
cout << "s1:" << s1 << endl;
string s2("welcom to C++");
s2.resize(20, 'x');
cout << "s2:" << s2 << endl;
s1.resize(5);
cout << "s1:" << s1 << endl;
}
int main()
{
test();
return 0;
}
运行结果:
part 3
void reserve(size_t n = 0);
std::string::reserve(size_t n)
函数用于请求字符串分配足够的内存空间来存储至少n
个字符(不包括终止的空字符'\0'
)。这并不意味着字符串的大小(即其中包含的字符数)会增加,它仅仅是预先分配了内存,以避免在未来添加字符时频繁地重新分配内存。这样做可以提高性能,因为内存分配通常是一个昂贵的操作。
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1("hello");
cout << "s1.size:" << s1.size() << endl;
cout << "s1.capacity:" << s1.capacity() << endl;
s1.reserve(20);
cout << "After reserve s1.size:" << s1.size() << endl;
cout << "After reserve s1.capacity:" << s1.capacity() << endl;
}
int main()
{
test();
return 0;
}
运行结果:
reserve之后的capacity为31,是因为vs自己的机制导致的,不同的编译器结果可能会不一样。
与访问和遍历有关的函数
1)char& operator[] (size_t pos);
2)const char& operator[] (size_t pos) const;
3)iterator begin();
4)const_iterator begin() const;
5)iterator end();
6)const_iterator end() const;
const char& operator[] (size_t pos) const,const_iterator begin() const,const_iterator end() const,const修饰*this,即this指针指向的内容,所以this指针指向的内容不可修改,用法上和其他函数是一样的。
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1("hello world!");
cout << s1[2] << endl;//类似数组的下标访问
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
}
int main()
{
test();
return 0;
}
运行结果:
与修改有关的函数
1)string& operator+= (const string& str);
2)string& operator+= (const char* s);
3)string& operator+= (char c);
4)string& append(const char* s);
5)void push_back(char c);
6)string& insert(size_t pos, const char* s);
7)string& insert(size_t pos, size_t n, char c);
8)string& erase(size_t pos = 0, size_t len = npos);
这里列举了较多的函数,但其实用法都大致相同,很容易做到举一反三,所以这里就不一一举例了。
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1("hello ");
cout << "s1:" << s1 << endl;
s1 += "world";
cout << "s1 after +=: " << s1 << endl;
cout << endl;
string s2("Urspeacil");
cout << "s2: " << s2 << endl;
s2.append("chenyilan");
cout << "s2 after append: " << s2 << endl;
cout << endl;
string s3("yyds");
cout << "s3: " << s3 << endl;
s3.push_back('y');
cout << "s3 after push_back: " << s3 << endl;
cout << endl;
string s4("world peace");
cout << "s4: " << s4 << endl;
s4.insert(0, "I hope ");
cout << "s4 after insert: " << s4 << endl;
cout << endl;
string s5("boy");
cout << "s5: " << s5 << endl;
s5.insert(1, 1, 'o');
cout << "s5 after insert: " << s5 << endl;
cout << endl;
string s6("I love you!");
cout << "s6: " << s6 << endl;
s6.erase(6);
cout << "s6 after erase: " << s6 << endl;
}
int main()
{
test();
return 0;
}
运行结果:
npos
npos是一个静态成员常量,表示一个不可能的位置。
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "npos: " << string::npos << endl;
return 0;
}
运行结果:
string& erase(size_t pos = 0, size_t len = npos)上面讲的的这个函数,形参len就给了npos为缺省值(默认值),表示如果不指定形参len,那么将删除pos位置到字符串结尾的字符。
与string相关的其它常用函数
1)const char* c_str() const;
2)size_t find(char c, size_t pos = 0) const;
3)size_t rfind(char c, size_t pos = npos) const;
4)string substr(size_t pos = 0, size_t len = npos) const;
c_str() ,它主要用于将字符串类型的字符数组(包括C++字符串和C字符数组)转换为C类型字符串,即以'\0'结尾的字符数组。
substr(),用于从字符串中提取子串。其函数原型为:std::string substr (size_t pos = 0, size_t len = npos) const;
,其中pos是子串的起始位置,len是子串的长度。如果len为std::string::npos或省略该参数,则子串将延续到原始字符串的末尾。
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s1("hello world");
cout << "c_str: " << s1.c_str() << endl;
//找s1中的第一次出现的o并返回下标,可以用find()正着遍历s1
cout << "find: " << s1.find('o') << endl;
//找s1中的最后出现的o并返回下标,可以用rfind()反着遍历s1
cout << "rfind: " << s1.rfind('o') << endl;
string s2 = s1.substr(0, 5);
cout << "s2: " << s2 << endl;
}
int main()
{
test();
return 0;
}
运行结果:
常用非成员函数
1)istream& operator>> (istream& is, string& str);//流提取
2)ostream& operator<< (ostream& os, const string& str);//流插入
3)istream& getline(istream& is, string& str);
流插入和流提取的使用很简单,这里就不赘述了,讨论getline和cin的区别更有意义。
getline和cin的区别
#include <iostream>
#include <string>
using namespace std;
void test()
{
string str;
cin >> str;//输入world peace
cout << str;
}
int main()
{
test();
return 0;
}
运行结果:
可以看到,peace并未被读取。原因在于:cin在遇到空白字符(空格、制表符、换行符)时会停止读取,导致空白字符后面的内容无法读取。
下面看看getline的表现:
#include <iostream>
#include <string>
using namespace std;
void test()
{
string str;
getline(cin, str);//输入world peace
cout << str;
}
int main()
{
test();
return 0;
}
运行结果:
可以看到,getline可以读取到空格后面的内容,原因在于getline一次从输入流中读取一行,直到遇到换行符(\n)才停止读取。
完~