【C++】STL中的string类

一.常见构造接口

  • string(); 空对象
  • string(const char* s); s字符串对象
  • string (size_t n,char c); n个c的字符串对象
  • string(const string& s); 拷贝构造函数
void teststring() {
	string s1;//string (); 
	string s2("hello");//string(const char* str);
	string s3(10, '$');//string(size_t n,char c);
	string s4(s3);//string(const string& str);
	cin >> s1;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s1 << endl;
}

二.容量操作接口

  • 小于15个字符,则使用静态顺序表存储(15)。
  • 若大于15,则使用动态顺序表

1.容量检测接口

  • size()/length() 检测字符串长度
  • capacity() 检测底层开辟空间大小
  • empty() 判断开辟空间是否存放内容
  • clear() 清空字符串内容
void teststring2() {
	string s("hello");
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;//
	//size()与 length()用法相同.size()只是为了与其他容器保持名称一致,一般用size()
	s.clear();//只清空内容,不改变底层空间容量大小
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
}

2.容量修改函数接口

  • resize()
//注意:1.如果是缩小有效元素,只改变有效元素的个数。不改变底层空间容量大小
//2.如果是扩大有效元素,改变有效元素的个数。有可能改变底层空间容量大小
void teststring3() {
	string s("hello");
	cout << s<< endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.resize(10, '!');
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.resize(20, '8');
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.resize(6);
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
}
  • reservr()
//1.主要用来进行扩容。
//缩小有效元素个数,不改变。小于15时,改变为15.
//小于十五个,分配静态数组
//大于十五时,动态链表
void teststring4() {
	string s("hello");
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.reserve(20);
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.reserve(40);
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.reserve(24);
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
	s.reserve(10);
	cout << s << endl;
	cout << s.size() << " " << s.capacity() << " " << s.empty() << endl;
}

三.元素访问及修改接口

1.访问

  • []操作符
  • at()函数
  • 都是元素访问接口,唯一不同是越界处理。
void teststring5() {
	string s("hello");
	cout << s[0] << endl;
	s[2] = 'L';//如果越界,assert触发,代码崩溃
	s.at(4) = 'O';//越界,抛出异常。
	cout << s[2] << endl;
	cout << s[4] << endl;
}

  • begin() 返回字符串开始位置地址
  • end() 返回字符串结束位置地址

2.追加

  • push_back
  • append
  • +=操作符
//追加   +=就够用了
//push_back  append  +=三种方式
//push_back +=  用法相同   append用途较多
void teststring6() {
	string s1("hello ");
	s1.push_back('i');
	s1 += " love ";
	string s2("you");
	s1 += s2;
	s1.append(1, ',');
	s1.append("祖国");
	s1.append(3, '!');
	cout << s1 << endl;
}
  • insert()
  • 删除之后更新。
    3.转格式
  • c_str() 转化为c格式字符串
void teststring7() {
	string s("12345");
	int ret = atoi(s.c_str());//转换字符串到整型,参数需要c格式字符串
	// c函数  将string转为int
}

4.查找

  • find 正向查找
  • rfind 反向查找
  • 对象 c格式字符串 字符 都可以查找
//find
void teststring8() {
	string s("hello world ");
	size_t pos1 = s.find(' ');  //返回位置
	size_t pos2 = s.find(' ',7);
	cout << pos1 << " " << pos2 << endl;
}
//rfind
void teststring9() {
	string s("hello world ");
	size_t pos1 = s.rfind(' ');  //返回位置
	size_t pos2 = s.rfind(' ', 2);
	cout << pos1 << " " << pos2 << endl;
}

5.字串

  • substr() 生成字串。从pos位置开始截取。
void teststring10() {
	string s("hello world.cpp.cpp ");
	size_t pos2 = s.rfind('.')+1;
	string fileout = s.substr(pos2);
	cout << fileout << endl;
}

四.迭代器

1.三种遍历方式

void teststring11() {
	string s("hello");
	//范围for
	for (auto e : s)
		cout << e;
	cout << endl;
	//普通遍历  
	for (size_t i=0;i<s.size();i++)
		cout << s[i];
	cout << endl;
	//迭代器方式  就是指针 char*
	string::iterator it = s.begin();
	while (it != s.end()) {
		cout << *it;
		it++;
	}
}

五.其他函数接口

  • getline() 用于获取一整行字符串 包括空格。
s = getline(cin, s);//获取从键盘输入的一行字符串
  • reverse(s.begin(), s.end())函数 用于逆置[begin,end)的字符串
void reversestring(string& s) {
		cout << s;
	cout << endl;
	reverse(s.begin(), s.end());//逆置函数    [begin,end)
		cout << s;
}
发布了53 篇原创文章 · 获赞 49 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43550839/article/details/101621918