STL (string 容器)

3. 常用容器

3.1 string容器

3.1.1 string容器基本概念

C风格字符串(以空字符结尾的字符数组)太过复杂难于掌握,不适合大程序的开发,所以C++标准库定义了一种string类,定义在头文件<string>。

String和c风格字符串对比:

  1. Char*是一个指针,String是一个类

string封装了char*,管理这个字符串,是一个char*型的容器。

  1. String封装了很多实用的成员方法

查找find,拷贝copy,删除delete 替换replace,插入insert

  1. 不用考虑内存释放和越界

 string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

3.1.2 string容器常用操作

3.1.2.1 string 构造函数

string();//创建一个空的字符串 例如: string str;     

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

string(const char* s);//使用字符串s初始化

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

 

3.1.2.2 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);//把字符串s的前n个字符赋给当前的字符串

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

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

string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串

 

3.1.2.3 string存取字符操作

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

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

 

3.1.2.4 string拼接操作

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

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

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

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

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

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

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

string& append(int n, char c);//在当前字符串结尾添加n个字符c

 

3.1.2.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

 

3.1.2.6 string比较操作

/*

compare函数在>时返回 1,<时返回 -1,==时返回 0。

比较区分大小写,比较时参考字典顺序,排越前面的越小。

大写的A比小写的a小。

*/

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

int compare(const char *s) const;//与字符串s比较

3.1.2.7 string子串

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

 

3.1.2.8 string插入和删除操作

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

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个字符

 

 

3.1.2.9 string和c-style字符串转换

//string 转 char*

string str = "itcast";

const char* cstr = str.c_str();

//char* 转 string

char* s = "itcast";

string str(s);

 

提示:

  在c++中存在一个从const char*到string的隐式类型转换,却不存在从一个string对象到C_string的自动类型转换。对于string类型的字符串,可以通过c_str()函数返回string对象对应的C_string.

  通常,程序员在整个程序中应坚持使用string类对象,直到必须将内容转化为char*时才将其转换为C_string.

 

提示:

   为了修改string字符串的内容,下标操作符[]和at都会返回字符的引用。但当字符串的内存被重新分配之后,可能发生错误.

 

    string s = "abcdefg";

    char& a = s[2];

    char& b = s[3];

 

    a = '1';

    b = '2';

 

    cout << s << endl;

    cout << (int*)s.c_str() << endl;

 

    s = "pppppppppppppppppppppppp";

 

    //a = '1';

    //b = '2';

 

    cout << s << endl;

    cout << (int*)s.c_str() << endl;


02.string 容器
  构造  赋值assign
    对字符存取[],at 区别
    []访问越界,直接挂掉
    at抛出异常 out_of_range
    substr 配合find查找邮件用户名
    char * string互转
    char* 隐式转换 string反之不可以
    转大写toupper  转小写 tolower
    find 如果找不到返回-1  找到返回第一次出现的位置


#include <iostream>
#include <string>
#include <stdexcept>//使用系统标准异常需要添加头文件
using namespace std;





/*
1 string 构造函数
string();//创建一个空的字符串 例如: string str;
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化

2 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);//把字符串s的前n个字符赋给当前的字符串
string& assign(const string &s);//把字符串s赋给当前字符串
string& assign(int n, char c);//用n个字符c赋给当前字符串
string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串

*/


void test01() {
	//要包含命名空间std;
	
	string str;//默认构造
	string str2(str);//拷贝构造
	string str3 = str;

	string str4 = "abcd";
	string str5(10, 'a');


	cout << str4 <<endl<< str5 << endl;
	
	//基本的赋值
	
	str = "hello";
	str2 = str4;
	

	//重载版本
	//string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串

	str3.assign("abcde", 4);
	cout << str3 << endl;
	//string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串
	string str6;
	str6.assign(str, 1, 3);//从0索引 输出ell
	cout << str6 << endl;
	   
}

/*
3 string存取字符操作
char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符
*/


void test02() {
	//两种方式

	string s = "hello world";
	for (int i = 0; i < s.size(); i++)
	{
		//cout << s[i] << endl;
		cout << s.at(i)<<endl;
	}

	//[]和at 区别?
	//[]访问越界 直接挂掉
	//at会抛出异常

	try {

		//cout << s[100] << endl;
		cout << s.at(100) << endl;
	}
	catch (out_of_range& e) {
		cout << e.what() << endl;
		//无效的字符串的位置
	}
	catch (...) {
		cout << "越界异常" << endl;
	}



}


/*
4 string拼接操作
string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=()
string& append(const string &s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c
append和+=一样


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

*/

void test03() {
	//拼接操作
	string s1 = "我";
	string s2 = "爱北京";
	s1 += s2;
	
	s1.append("天安门");
	cout << s1 << endl;

	//查找操作  find
	//普通find 从左往右找
	//rfind 从右往左找

	string s = "abcdefgbc";
	int pos = s.find("bcf");//按b开始位置查找
	//找到返回位置  找不到返回-1  这是string容器,其他容器可能还不一样

	cout << "pos= " << pos << endl;
	int pos2 = s.rfind("bc");
	cout << "pos2= " << pos2 << endl;//7
	//从右往左查找第一次出现的位置
	//rfind和find内部查找顺序相反



	//替换操作
	string s3 = "hello";
	s3.replace(1,3,"1111111");//从1开始3 个字符被替换成了1111111
	cout << s3 << endl;//h111o 
	



}

/*
6 string比较操作
/*
compare函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的A比小写的a小。

int compare(const string& s) const;//与字符串s比较
int compare(const char* s) const;//与字符串s比较
*/
void test04() {
	string s1 = "abc";
	string s2 = "aac";
	if (s1.compare(s2) == 0) {//按ascii码比较
		cout << "s1等于s2";
	}
	else if(s1.compare(s2) == 1 )
	{
		cout << "s1大于s2";
	}
	else {
		cout << "s1小于s2";
	}
}

/*
7 string子串
string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
*/
void test05(){
	string s1 = "abcde";
	string s2 = s1.substr(1, 3);//1开始取3个
	cout << "s2= " << s2 << endl;

	//需求: 查找一个右键的用户名
	string email = "xiaoxiao@189.com";
	int pos=email.find("@");//8
	cout << "pos= " << pos << endl;

	string usrName = email.substr(0, pos);
	cout << "用户名为:" << usrName << endl;

}

/*
8 string插入和删除操作
string& insert(int pos, const char* s); //插入字符串
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  test06() {
	string s1 = "hello";
	s1.insert(1, "1111");
	cout << "s1:" << s1 << endl;

	//删除1111
	s1.erase(1, 4);
	cout << s1 << endl;

}


/*
9 string和c-style字符串转换
//string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
//char* 转 string
char* s = "itcast";
string str(s);

*/

void func(string s) {
	cout << s << endl;
}

void func2(char* s) {
	cout << s << endl;
}

void test07() {
	string s = "abc";
	//string-> const char*
	const char* p = s.c_str();
	//const_cast z转非常量的char *


	func(p);//隐式转换为string

	//const char * -> string
	string s2(p);//构造的方式
	//func2(s2);//string 不能隐式类型转换为char*

}
/*
提示:
  在c++中存在一个从const char*到string的隐式类型转换,却不存在从一个string对象到C_string的自动类型转换。对于string类型的字符串,可以通过c_str()函数返回string对象对应的C_string.
  通常,程序员在整个程序中应坚持使用string类对象,直到必须将内容转化为char*时才将其转换为C_string.


 
 提示:
   为了修改string字符串的内容,下标操作符[]和at都会返回字符的引用。但当字符串的内存被重新分配之后,可能发生错误.


	string s = "abcdefg";
	char& a = s[2];
	char& b = s[3];

	a = '1';
	b = '2';

	cout << s << endl;
	cout << (int*)s.c_str() << endl;

	s = "pppppppppppppppppppppppp";

	//a = '1';
	//b = '2';

	cout << s << endl;
	cout << (int*)s.c_str() << endl;


*/


void test08() {
	string s = "abcdefg";
	char& a = s[2];
	char& b = s[3];

	a = '1';
	b = '2';

	cout << s << endl;
	cout << (int*)s.c_str() << endl;

  //内存不够,重新分配内存
	s = "pppppppppppppppppppppppp";
	//原有的引用失效
	
	//a = '1'//直接赋值会异常;
	//b = '2';

	cout << s << endl;
	cout << (int*)s.c_str() << endl;

}

/*
小练习
写一个函数,函数内部将string字符串中的所有小写字母都变为大写字母。
*/
void test09() {
	string s = "aBcdEfg";

	for (int i = 0; i < s.size(); i++) {
		//s[i]=toupper(s[i]);//变大写
		s[i] = tolower(s[i]);//变小写

	}
	cout << s << endl;

}



int main()
{
	//test01();
	//test02();
	//test03();
	//test04();
	//test05();
   //test06();
	//test07();
	//test08();
	test09();
}

 

 

 

 

 

 

 

 

 

 

 

 

3.1.3 小练习

写一个函数,函数内部将string字符串中的所有小写字母都变为大写字母。

/*
	create by XXX
	2017 4 26
	文件 功能。。。

	func()。。。干嘛的  参数1 。。。
	方便别人 方便自己  写文档 写注释
*/

#include<iostream>
using namespace std;
#include <vector>
#include <string>
#include <deque>
#include <algorithm>
#include <ctime>
/*
  有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均分。
//1. 创建五名选手,放到vector中
//2. 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容器中
//3. sort算法对deque容器中分数排序,pop_back pop_front去除最高和最低分
//4. deque容器遍历一遍,累加分数,累加分数/d.size()
//5. person.score = 平均分
*/

class Person
{
public:
	Person(string name, int score)
	{
		this->m_Name = name;
		this->m_Socre = score;
	}

	string m_Name; //人名
	int m_Socre; //分数 平均分
};


void createPerson(vector<Person>& v)
{
	string nameSeed = "ABCDE";
	for (int i = 0; i < 5; i++)
	{
		string name = "选手";
		name += nameSeed[i];

		int score = 0;
		Person p(name, score);

		v.push_back(p);
	}
}


void setScore(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{

		//对5个人进行打分
		deque<int>d;
		for (int i = 0; i < 10; i++)
		{
			int score = rand() % 41 + 60; //60 ~ 100

			d.push_back(score);
		}

			//先看下打分
			//for (deque<int>::iterator dit = d.begin(); dit != d.end();dit++)
			//{
			//	cout << *dit << " ";
			//}
			//cout << endl;

		//排序 从小到大
		sort(d.begin(), d.end());

		//去除 最高 和 最低
		d.pop_back(); //最高
		d.pop_front();

		int sum = 0;//总分
		for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
		{
			sum += *dit;
		}

		//平均分
		int avg = sum / d.size();

		it->m_Socre = avg;
	}

}

void showSocre(vector<Person>& v)
{
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名:" << it->m_Name << " 最终平均分为: " << it->m_Socre << endl;
	}

}


int main() {

	//设置随机数种子
	srand((unsigned int)time(NULL));

	
	
	//创建容器  存放 选手
	vector<Person>v;

	//创建5名选手
	createPerson(v);

	//打分
	setScore(v);

	//展示平均分
	showSocre(v);


	//测试
	//for (vector<Person>::iterator it = v.begin(); it != v.end();it++)
	//{
	//	cout << "姓名:" << (*it).m_Name << endl;
	//}




	system("pause");
	return EXIT_SUCCESS;
}

(本笔记内容整理自网络资源,侵删)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值