C++:STL:常用容器(上):string

1:string容器

1.1 string基本概念

本质: string是C++风格的字符串,而string本质是一个类

string和char* 区别

1:char* 是一个指针

2:string是一个类,类内部封装了 char*  管理这个字符串,是一个 char* 型的容器

特点:

1:string类内部封装了很多成员方法,比如:差找find ,拷贝copy ,删除 delete,替换 replace,插入 insert

2:  string管理char* 所分配的内存,不用担心复制越界和取值越界,由类内部进行负责

1.2 string构造函数 

string() ------》创建一个空的字符串, string str

string(const char* c) ---->使用字符串 s 进行初始化

string(const string& str) -----》使用一个string对象初始化另一个string对象

string(int n, char c) ----->使用 n个字符 c 初始化

 案例:用四种构造函数来实例化 string 对象

#include<iostream>
#include<string>
using namespace std;

void test() {
	string s1; // 创建空字符串,调用无参构造函数
	
	const char* s = "hello world";
	string s2(s);

	string s3(s2);

	string s4(10, 'a');

	cout << "s1 = " << endl;
	cout << "s2 = " << s2<< endl;
	cout << "s3 = " << s3 << endl;
	cout << "s4 = " << s4<< endl;
}
int main() {
	test();
}

1.3 :string赋值操作 

功能描述:给 string字符串进行赋值

赋值函数原型:

string& operator=(const char* s)  // char* 类型字符串赋值给当前的 字符串

string& operator=(const string& s)  // 字符串s赋值给当前的字符串

string& operator=(char c) // 字符赋值给当前的字符串

string& assign(const char* s) // 把字符指针s 赋值给当前字符串

string& assign(const char*s ,int n) // 把字符指针前n 个字符,赋值给当前字符串

string& assign(const string& s) // 当字符串s 赋值给当前字符串

string& assign(int n, char c) // 用 n个字符 c 赋值给当前字符串

#include<iostream>
#include<string>
using namespace std;

void test() {
	string s1; // 创建空字符串,调用无参构造函数
	
	const char* s = "hello world";
	string s2(s);

	string s3(s2);

	string s4(10, 'a');

	cout << "s1 = " << endl;
	cout << "s2 = " << s2<< endl;
	cout << "s3 = " << s3 << endl;
	cout << "s4 = " << s4<< endl;
}

void test2() {
	string s1;
	s1 = "hello world";
	cout << "s1 = " << s1 << endl;

	string s2;
	s2 = s1;
	cout << "s2 = " << s2 << endl;

	string s3;
	s3 = 'a';
	cout << "s3 = " << s3 << endl;

	string s4;
	s4.assign("hello world");
	cout << "s4 = " << s4 << endl;

	string s5;
	s5.assign("hello,world", 5);
	cout << "s5 = " << s5 << endl;

	string s6;
	s6.assign(s5);
	cout << "s6 = " << s6 << endl;

	string s7;
	s7.assign(10, 'a');
	cout << "s7 = " << s7 << endl;
}
int main() {
	test();
}

 

1.4:string字符串拼接 

功能描述: 实现字符串末尾拼接字符串

函数原型:

string&  operator+=(const char* str)   // 重载+=操作符

string&  operator+=(const char c)  // 重载+=操作符

string&  operator+=(const string& str) // 重载+=操作符

string& append(const char* s) //把字符串s 连接到当前字符串结尾

string& append(const char* s,  int n) // 把字符串s 的前n个字符连接到当前字符串结尾

string& append (const string& s)  //  同 operator+=(const string& str)

string& append (const string& s , int pos , int n)   // 字符串s ,从pos开始的 n个字符连接到字符串结尾。

#include<iostream>
#include<string>
using namespace std;

void test() {
	string s1; // 创建空字符串,调用无参构造函数
	
	const char* s = "hello world";
	string s2(s);

	string s3(s2);

	string s4(10, 'a');

	cout << "s1 = " << endl;
	cout << "s2 = " << s2<< endl;
	cout << "s3 = " << s3 << endl;
	cout << "s4 = " << s4<< endl;
}

void test2() {
	string s1;
	s1 = "hello world";
	cout << "s1 = " << s1 << endl;

	string s2;
	s2 = s1;
	cout << "s2 = " << s2 << endl;

	string s3;
	s3 = 'a';
	cout << "s3 = " << s3 << endl;

	string s4;
	s4.assign("hello world");
	cout << "s4 = " << s4 << endl;

	string s5;
	s5.assign("hello,world", 5);
	cout << "s5 = " << s5 << endl;

	string s6;
	s6.assign(s5);
	cout << "s6 = " << s6 << endl;

	string s7;
	s7.assign(10, 'a');
	cout << "s7 = " << s7 << endl;
}

void test3() {
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1 = " << str1 << endl;
	str1 += ':';
	cout << "str1 = " << str1 << endl;

	string str2 = "王者荣耀";
	str1 += str2;
	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append("Love");
	cout << "str3 = " << str3 << endl;

	str3.append("game  aaaaa",4);
	cout << "str3 = " << str3 << endl;

	str3.append(" ").append(str2);
}

int main() {
	test2();
}

 1.5:string查找和替换

功能描述:

查找: 查找指定字符串是否存在

替换: 在指定的位置替换字符串

函数原型

int find(const string& str , int pos = 0) const    // 查找str第一次出现位置,从pos 开始查找

int find(const char* s, int pos = 0) const       // 查找s 第一次出现位置,从pos开始查找

int find(const char* s, int pos, int n)  const   // 从pos位置查找s 的前 n 个字符第一次位置

int find(const char c , int pos = 0) const  // 查找字符c 第一次出现的位置

int rfind(const string& str,  int pos = npos)  const   // 查找str 最后一次出现的位置,从pos 开始查找

int  rfind(const char* s , int pos = npos) const    // 查找 s 最后一次出现的位置,从pos 开始查找

int rfind(const char* s, int pos , int n) const  // 从pos位置查找  s 的前n个字符最后一次 出现 位置

int rfind(const char c , int pos = 0) const  // 查找字符c最后一次出现位置

string&  replace(int pos, int n , const string& str)  // 替换从pos位置 开始 n个字符为 字符串 str

string& replace(int pos,  int n , const char* s) // 替换从 pos位置 开始的 n 个字符为 字符串 s

注意  

find 查找是从左往右, rfind 是从右往左

find 找到字符串后返回查找的第一个字符位置,找不到返回 -1 

replace 在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

案例:测试字符串的查找和替换功能 

void test4() {
	cout << "字符串查找" << endl;
	string str1 = "abcdefgde";
	int pos;
	pos = str1.find("de");
	cout << "de 的位置在:" << pos << endl;

	pos = str1.find("UI");
	cout << "UI 的位置在:" << pos << endl;

	pos = str1.rfind("de");
	cout << "de 的位置在:" << pos << endl;

	cout << "字符串替换" << endl;
	str1.replace(1, 3, "1111");
	cout << "str1 = " << str1 << endl;
	
}

1.6:字符串比较 

功能描述:字符串之间的比较,主要是用于比较两个字符串是否相对,判断谁大谁小的意思 并不是很大。

比较方式:按照字符的  ASCII码逐个进行比较

= :两个祖父串完全相等,返回 0

> :  第一个字符串的首个 字符大于第二个字符串的首个字符,返回 1

< :  第一个字符串的首个字符小于第二个字符串的首个字符,返回-1

函数原型

int  compare(const string& s)const    // 与字符串s进行比较

int compare(const char*  chr) const    // 与字符串 chr 进行比较

案例:测试字符串的比较功能

void test5() {
	string str1 = "hello";
	string str2 = "xello";
	if (str1.compare(str2)==0) 
	{
		cout << "两个字符串相等" << endl;
	}
	else if (str1.compare(str2)<0)
	{
		cout << "第一个字符串大于第二个字符串" << endl;
	}
	else
	{
		cout << "第一个字符串小于第二个字符串" << endl;
	}
}

1.7 :string字符存取 

string单个字符存取方式有两种

char& operator[](int n)    // 通过[] 方式取字符

char& at(int n)   // 通过at 方式获取字符

void test6() {
	string str = "hello";
	cout << "取字符串中的字符" << endl;
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;

	cout << "修改字符串中的字符" << endl;
	str[0] = 'x';
	str.at(1) = 'x';
	cout << "str = " << str << endl;
}

 

1.8 : string插入和删除 

功能描述:对 string 字符串进行插入和删除字符操作

函数原型:

string&  insert(int pos , const char* chr)   //  插入字符

string&  insert(int pos, const string& str)  // 插入字符串

string&  insert(int pos, int n, char c) // 在指定位置插入 n 个字符c

string&  erase(int pos , int n = npos)  // 删除从 pos开始的n 个字符

 案例: 测试字符串的插入和删除功能

void test7() {
	cout << "字符串插入功能" << endl;
	string str = "hello";
	str.insert(1, "111");
	cout << "str = " << str << endl;

	string str2 = "222";
	str.insert(1, str2);
	cout << "str = " << str << endl;

	// 删除功能
	str.erase(2, 4);
	cout << "str = " << str << endl;

}

 

1.9 :字符串子串 

功能描述: 从字符串中获取想要的字串

函数原型: string substr(int pos = 0,int n = npos)const  // 返回由pos开始的 n 个字符组成的字符串

案例:测试字符串字串的功能

void test8() {
	cout << "字符串子串功能" << endl;
	string str = "abcdefg";
	string substr = str.substr(1, 3);
	cout << "子串为:" << substr << endl;

	string email = "yuhongwen@163.com";
	int pos = email.find('@');
	substr = email.substr(0, pos);
	cout << "用户名:" << substr << endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值