string类官方解释:
String objects are a special type of container, specifically designed to operate with sequences of characters. Unlike traditional c-strings, which are mere sequences of characters in a memory array, C++ string objects belong to a class with many built-in features to operate with strings in a more intuitive way and with some additional useful features common to C++ containers. The string class is an instantiation of the basic_string class template, defined in <string> as:
|
一、C++string类的一些知识点了解
1、C和C++字符串的转换
(1)C语言字符串转C++字符串
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(void)
{
string str("hello world");
cout << str << endl;
return 0;
}
(2)C++字符串转C语言字符串 c_str成员函数的使用
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main(void)
{
string str("hello C++");
const char *ss = str.c_str();
printf("%s\n",ss);
return 0;
}
2、capacity、size、length、resize成员函数的使用
// 1、capacity:创建对象后,动态开辟好的空间,目前容量的大小
//扩充机制:一般2倍每次的增容
int main()
{
string str("str");
cout << str.capacity() << endl; //本人电脑初始为15
return 0;
}
2、size、length:string容器内已存放的元素的个数,有别于capacity
3、resize:指定为string容器重新开辟空间的大小
综合使用:
int main(void)
{
string str("str");
cout << str.capacity() << endl; //15
cout << str.size() << ":" << str.length() << endl;
str.resize(100);
cout << str.size() << endl; //100
return 0;
}
3、max_size成员函数:指定string容器最大可以存放的容量,一般为1/2G
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(void)
{
string str("str");
cout << str.max_size() << endl;
return 0;
}
4、string容器的访问
分为两种访问方式:[下标] 和 at方法
1、[] : 通过数组的下标进行访问,如:a[2];
2、at成员方法:通过成员方法访问,如:ss.at(3);
两者区别:[]下标访问时,如果越界程序直接挂掉;
at方法,访问越界时,会抛出异常out_of_range
两者应用举例:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string ss("scascecaecec");
try
{
//cout << a[100] << endl; 访问越界,程序直接崩掉
cout << ss.at(100) << endl;
}
catch(...)
{
cout << "out of range" << endl;
}
return 0;
}
5、string类的赋值
1、定义对象时,调用构造函数赋初值
如:string ss("hanmenghao");
2、调用拷贝构造函数赋初值
如:string ss = s1;
3、使用赋值操作符重载赋值
如:string ss;
ss = s1;
4、assign成员函数
如:string ss;
ss.assign("hanmenghao"); //string提供的字符串赋值操作方法
6、字符串查找
(1)find :从前往后找第一项出现的位置 返回值:int
(2)rfind :从后往前找第一项出现的位置 返回值:int
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string ss("scascecaecec");
int pos = ss.find("as");
cout << pos << endl;
pos = ss.rfind("ce");
cout << pos << endl;
return 0;
}
7、字符串替换操作
replace成员函数:
一种简单用法:
ss.replace(开始下标, 结束下标, "替换字串");
应用举例:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string ss("scascecaecec");
ss.replace(0,7,"hanmengha");
cout << ss << endl;
return 0;
}
8、字符串比较
compare成员方法:
compare成员方法:
1、返回值 int
2、字串相等:返回0; 大于返回1; 小于返回-1;
应用举例:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string ss("scascecaecec");
string s2;
s2.assign("hello world!!!");
int ret = ss.compare(s2);
cout << ret << endl;
return 0;
}
9、字符串的插入和删除
(1)插入insert
格式:ss.insert(插入位置, "插入的子串");
注:insert在指定位置前方插入
(2)删除
1、erase方法:从字串中删除一个子串
格式:ss.erase(开始位置, 删几个);
2、clear方法:删除字串中所有的元素
综合应用:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string str("brvvnvlksDNVlkeWNVLWEVMSVSDVS");
str.insert(3," hello ");
cout << str << endl;
str.erase(10,10);
cout << str << endl;
str.clear();
cout << str.size() << endl; // 0
return 0;
}