C++基础——STL——String

String

需添加头文件,#include <string> 和 using namespace std;

1. 定义

string str;
// 初始化
string str = "abcd";

2. 访问

(1)下标

#include <stdio.h>
#include <string>
using namespace std;
int main(){
	string str = "abcd";
	for(int i = 0; i < str.length(); i++){
		printf("%c", str[i]);
	}
	return 0;
}

如果要 输入和输出 整个字符串,则 只能用 cin 和 cout

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str;
	cin>>str;
	cout<<str;
	return 0;
}

用 printf 输出 string,需用 c_str() 将 string 类型转换为 字符数组 进行输出

#include <stdio.h>
#include <string>
using namespace std;
int main(){
	string str = "abcd";
	printf("%s\n", str.c_str());
	return 0;
}

(2)迭代器

// 定义
string::iterator it;
//示例
#include <stdio.h>
#include <string>
using namespace std;
int main(){
	string str = "abcd";
	for(string::iterator it= str.begin(); it != str.end(); it++){
		printf("%c", *it);
	}
	return 0;
}

3. 常用函数

(1) operator +=

          string 的加法,可以将两个 string 直接拼接起来

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

(2)compare operator

         可以直接使用 ==、!=、<、<=、>、>= 比较大小,比较规则是 字典序

#include <stdio.h>
#include <string>
using namespace std;
int main(){
	string str1 = "aa", str2 = "aaa", str3 = "abc", str4 = "xyz";
	if(str1 < str2)
		printf("ok1\n");
	if(str1 != str3)
		printf("ok2\n");
	if(str4 >= str3)
		printf("ok3\n");
	return 0;
}

(3)length() / size(),返回 string 的长度

(4)insert() 

        ① insert(pos, string),在 pos 号位置插入字符串 string,示例:

string str = "abcxyz", str2 = "opq";
str.insert(3, str2);
cout<<str<<endl;

        ② insert(it, it2, it3),it 为 原字符串的欲插入位置,it2 和 it3 为待插字符串的首尾迭代器

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

(5)erase()

         ① 删除 单个 元素: str.erase(it),it 为需要删除的元素的迭代器

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(str.begin()+4);
	cout<<str<<endl;
	return 0;
}

          ② 删除一个 区间 内所有元素:

  • str.erase(first, last),first 为需要删除的区间的起始迭代器,last 为需要删除的区间的末尾迭代器的下一个地址,也即为删除 [first, last),示例:
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(str.begin(), str.begin()+3);
	cout<<str<<endl;
	return 0;
}
  • str.erase(pos, length),pos—开始删除的起始位置,length—删除的字符个数
#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(0, 3);
	cout<<str<<endl;
	return 0;
}

(6)clear()

         清空 string 中的数据

(7)substr()

         substr(pos, len),返回从 pos 号位开始、长度为 len 的子串

#include <iostream>
#include <string>
using namespace std;
int main(){
	string str = "Thank you for your smile.";
	cout << str.substr(0, 5) << endl;
	cout << str.substr(7, 3) << endl;
	cout << str.substr(19, 5) << endl;
	return 0;
}

(8)string::npos

         是一个常数,本身值为-1,作为 find 函数失配时的返回值。示例:

#include <iostream>
#include <string>
using namespace std;
int main(){
	if(string::npos == -1){
		cout << "-1 is true." << endl;
	}
	if(string::npos == 4294967295){
		cout << "4294967295 is also true." << endl;
	}
	return 0;
}

(9)find()

  • str.find(str2),当 str2 是 str 子串时,返回其在 str 中第一次出现的位置,如果 str2 不是 str 的子串,那么返回 string::npos
  • str.find(str2, pos),从 str 的pos号位置开始匹配 str2,.....
#include <iostream>
#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 know 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
#include <iostream>
#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/gsj9086/article/details/86647518