一、string类的简介
string 是C++中的字符串。 字符串对象是一种特殊类型的容器,专门设计来操作的字符序列。 不像传统的c-strings,只是在数组中的一个字符序列,我们称之为字符数组,而C + +字符串对象属于一个类,这个类有很多内置的特点,在操作方式,更直观,另外还有很多有用的成员函数。
二、string类函数的使用
首先,使用string前必须包含头文件,加入#include<string.h>和using namespace std; 才能使用string.
1.string类的各重载构造函数使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//string的构造函数
string str1 = "hello";
string str2("world");
string str3(5,'c');
string str4(str2,1,3);
cout << "str1: " + str1 << endl;
cout << "str2: " + str2 << endl;
cout << "str3: " + str3 << endl;
cout << "str4: " + str4 << endl;
}
运行结果:
2.string operator+函数的各重载函数使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//string的运算符
string str1 = "abc";
string str2 = "edf";
char * str3 = "hello";
char c1 = '!';
string str12 = str1 + str2; //string operator+(const string& s1, const string& s2 );
string str13 = str1 + str3; //string operator+(const char* s, const string& s2 );
string str1c1 = str1 + c1; //string operator+( const string& s1, char c );
cout << str12 << endl;
cout << str13 << endl;
cout << str1c1 << endl;
}
运行结果:
3.string类的关系运算符使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "abc";
string str2 = "aac";
string str3 = "adc";
string str4 = "abc";
if (str1 > str2)
cout << "str1大于str2" << endl;
if (str1 >= str2)
cout << "str1大于或等于str2" << endl;
if (str1 < str3)
cout << "str1小于str3" << endl;
if (str1 <= str4)
cout << "str1小于或等于str4" << endl;
if (str1 == str4)
cout << "str1等于str4" << endl;
}
运行结果:
4.string类的下标运算符和at:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "hello";
char a = str[0];
char b = str.at(1);
cout << a << endl;
cout << b << endl;
}
运行结果:
5.string类的各重载append函数使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "aaa";
string t1 = "abc";
char * t2 = "bbb";
string str1 = str.append(t1);
string str2 = str.append(t2);
string str3 = str.append(t2,2);
string str4 = str.append(t1,2,3);
string str5 = str.append(4,'!');
string str6 = str.append(t1.begin() + 1,t1.end() - 1);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
cout << str6 << endl;
}
运行结果:
6.string类的各重载assign函数的使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "abc";
char * t1 = "efg";
string t2 = "bcde";
string str1 = str.assign(t1);
string str2 = str.assign(t1,2);
string str3 = str.assign(t2,1,3);
string str4 = str.assign(4,'!');
string str5 = str.assign(t2.begin(),t2.end());
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
}
运行结果:
7.string类的find系列函数的使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "There are a test!";
char * t = "test";
int index1 = str.find('a');
int index2 = str.find('e',3);
int index3 = str.find(str,5);
int index4 = str.find(t,0);
int index5 = str.find(t);
int index6 = str.find_first_of('e');
int index7 = str.find_first_of('e',3);
int index8 = str.find_first_of("are");
int index9 = str.find_first_not_of('e');
int index10 = str.find_first_not_of("There");
int index11 = str.find_last_of('e');
int index12 = str.find_last_not_of('e');
cout << index1 << endl;
cout << index2 << endl;
cout << index3 << endl;
cout << index4 << endl;
cout << index5 << endl;
cout << index6 << endl;
cout << index7 << endl;
cout << index8 << endl;
cout << index9 << endl;
cout << index10 << endl;
cout << index11 << endl;
cout << index12 << endl;
}
运行结果:
8.string类的insert函数使用:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "hello";
//str.insert("fff");
string str1 = str.insert(0,"aa");
string str2 = str.insert(1,"abc",4);
string str3 = str.insert(0,"efg",1,2);
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
}
运行结果:
其它函数因没有重载函数且参数较少或没有参数,所以只做简单说明,就不举例了。
9.string的begin()和end()就是返回字符串的开始和结束游标。
10.string的cstr()返回c风格的字符串
11.string的copy()函数就是复制字符串
12.string的clear()函数就清空字符串
13.string的erase()函数删除指定位置的字符或字符串
14.string的empty()函数是判断字符串是否为空
15.string的getline()函数是读一行字符串流
16.string的length()函数是返回字符串的长度
...后续更新