STL容器(string)

总结

1)string 就是一个不定长字符串,

2)string 很有用的就是可以 + 可以赋值为 空串,可以直接用 > <  == 等符号直接比较大小,判断两个字符串是否相等

3)求长度。str.size(), str.length()

4)函数

  1. insert()
  2. erase()
  3. clear()
  4. substr()
  5. find()
  6. replace()

什么是string?

我们学习过c语言中的char str[]来存放字符串,但是使用字符数组有时会显得操作麻烦,而且容易因为经验不足产生错误(你数组总是有大有小把,这时就会有越界问题存在)C++发现了这个问题,就有写了一个封装的string类型,对字符串常用的需求功能进行了分装,使得操作起来更方便,而且不易出错。

String的常见用法

第一当然是头文件了

#include<string>
using namespace std;

1)定义 与基本的数据类型相同,只需要在string后面个数变量名字即可

string str;    //定义一个string
string str = "abcd"    //初始化

2)访问

(1)通过下标,可以直接像字符数组那样去访问string。

#include<iostream>
#include<string>
using namespace std;
int main (){
	string str = "abcd";
	for(int i = 0; i < str.length(); i++){
		cout << str[i] << " ";
	}
	return 0;
}

 读入整个和输出整个字符串能用 cin  cout

当然还有其他方法:https://blog.csdn.net/Harington/article/details/83544175

string str;
cin >> str;
cout << str;

(2)通过迭代器访问

因为有insert()和erase()函数需要以迭代器作为参数,所以稍微学一下。

string::iterator it;

生成迭代器 it

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str = "abcd";
	for(string::iterator it = str.begin(); it != str.end(); it++){
		cout << *it << " ";
	}
	return 0;
}

常见函数

1)operator+=;

这个功能就很强了。我觉得简直是做题的福音, 让string 具有+法运算, 可以将两个string拼起来

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str1 = "abcd";
	string str2 = "xyz";
	string str3;
	str3 = str1 + str2;
        str2 += str1;
	cout << str3 << endl;
        cout << str2 ;
	return 0;
}

2)compare operator

可以直接用 > < >= <= ==    != 来比较大小,按照字典序比较。

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str1 = "a";
	string str2 = "aa";
	string str3 = "abc";
	string str4 = "xyz";
	if(str1 < str2) cout << "OK1" << endl;
	if(str1 != str3) cout << "OK2" << endl;
	if(str4 >= str3) cout << "OK3" << endl; 
	return 0;
}

3)length(),size()

字符串长度

4)insert()

时间复杂度为 O(N)

(1)insert(pos,string)在pos号位置插入字符串string

string str1 = "abcd";
string str2 = "opa";
str1.insert(3, str2);

(2)insert(it,It2, it3) it2 ,和it3为待插字符串的首位迭代器。用来表示串[ it2, it3)将被插在it位置上,是不是听不懂,看代码

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str = "abcdxyz";
	string str2 = "opu";
	str.insert(str.begin() + 3, str2.begin(), str2.end());
	cout << str << endl;
	return 0;
}

(5)erase()

删除单个或者删除一个区间内所欲元素,好像所有STL容器都有这个东西。

  • 删单
string str = "abcdxyz";
str.erase(str.begin() + 3);
  • 删区间内所有元素
  1. string(first, last)其中first 为需要删除的区间的起始迭代器,而last则为需要删除的区间的末尾迭代器的下一个地址,[first,lat)
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str = "abcdxyz";
	str.erase(str.begin() + 2, str.end() - 1);
	cout << str << endl;
	return 0;
}
  1. str.erase(pos, length) pos 为需要开始删除 的起始位置, length 为删除的字符的个数
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str = "abcdxyz";
	str.erase(3, 2);
	cout << str << endl;
	return 0;
}

(6)clear()

清空 时间复杂度O(1);

string str = "abcdxyz";
str.clear();

(7)substr(pos, len)

substr(pos, len)返回从pos号位开始,长度为len 的子串,时间复杂度为 O(len)

string str = "Thank you for your smile.";
cout << str.substr(0, 5) << endl;
cout << str.substr(14, 4) << endl;
cout << str.substr(19, 5) << endl;

(8)string::npos

string::npos是一个常数,其本身的值为 -1,但是由于unsigned_int 类型,因此也可以认为是 unsigned_int 类型的最大值,string::npos 等于 -1或者 4294967295.

(9)find()

str.find(str2),当str2, 是str的子串时,返回其在str中第一次出现的位置,如果str2不是str的子串,那么返回string::npos.

str.find(str2, pos),从str的 pos号位开始匹配str2,返回值与上相同。 时间复杂度O(nm) n ,m分别为 str str2的长度。

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str = "Thank you for your smile.";
	string str2 = "you";
	string str3 = "me";
	if(str.find(str2) != string::npos){
		cout << str.find(str2) << endl; 
	}
	if(str.find(str2, 7) != string::npos){
		cout << str.find(str2, 7) << endl;
	} 
	if(str.find(str3) != string::npos){
		cout << str.find(str3) << endl;
	}
	else{
		cout << "I konw there is no position for me." << endl; 
	}
	return 0;
}

(10)replace()

str.replace(pos, len, str2)把 str从pos号位开始,长度为Len的子串,替换为str2;

str.replace (it1, it2, str2)把str 迭代器,[ it1, it2 ) 范围的子串替换为 str2.

时间复杂度为O(str.length())

#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
int main (){
	string str = "Maybe you will turn around.";
	string str2 = "will not";
	string str3 = "surely";
	cout << str.replace(10, 4, str2) << endl;
	cout << str.replace(str.begin(), str.begin() + 5, str3) << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Harington/article/details/87871979
今日推荐