[C++]STL操作,string容器

#include<iostream>
#include<vector>  //动态数组(可变数组)
#include<algorithm> //算法头文件
using namespace std;

void PrintVector(int v) {
	cout << v << endl;
}



//STL基本语法
void text1() {
	vector<int> v;  //定义一个容器,指定容器存放int类型元素
	v.push_back(65);  //向容器尾部添加元素
	v.push_back(45);  //向容器尾部添加元素
	v.push_back(22);  //向容器尾部添加元素


	//通过STL提供的算法进行遍历
	//获取当前容器提供的迭代器
	vector<int>::iterator pBegin = v.begin();
	vector<int>::iterator pEnd = v.end();

	/*
	容器内可能存放基础数据类型,也可能存放自定义类
	第三个参数是回调函数,遍历个过程中每次都会调用这个函数
	对遍历的对象执行操作,当存放基础数据类型是,系统知道如何操作
	但如果存放的是自定义类,就需要回调函数指导系统操作
	*/
	for_each(pBegin, pEnd, PrintVector);
}




//容器也可以存放自定义数据类型
class Person {
public:
	Person(int age, int id) :age(age), id(id) {};
	int age;
	int id;
	void Show() {
		cout << "the age is " << age << " id is " << id << endl;
	}
};
void text2() {
	//创建自定义类的容器
	vector<Person> v;
	Person p1(10, 20), p2(20, 30), p3(15, 50);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	//自己利用迭代器进行遍历
	vector<Person>::iterator pBegin = v.begin();
	vector<Person>::iterator pEnd = v.end();
	while (pBegin != pEnd) {
		(*pBegin).Show();
		pBegin++;
	}
}



int main() {
	text2();

	return 0;
}

string容器

string容器特性

  1. Char* 是一个指针,string是一个类
  2. String封装了很多实用的成员方法
  3. 不用考虑内存释放和越界
  4. string和char*可以通过string类提供的c_str()方法转化
#include<iostream>
#include<vector>
using namespace std;

void test1() {
	string s1;  //无参构造
	string s2(10, 'a');  //含参构造
	string s3("hello");
	string s4(s3);  //拷贝构造

}


//string型多种赋值方法
void test2() {
	string s1;
	string s2("hello");
	s1 = "world";
	cout << s1 << endl;
	s1 = s2;
	cout << s1 << endl;
	s1 = "nihao";
	cout << s1 << endl;
	s1.assign("hihihi");
	cout << s1 << endl;
}




//string型取值操作
void text3() {
	string s1="helloworld";
	//重载了[]操作符
	for (int i = 0; i < s1.size(); i++) {
		cout << s1[i] << "   ";
	}
	cout << endl;
	//-----------------------------------------
	for (int i = 0; i < s1.size(); i++) {
		cout << s1.at(i) << "   ";
	}
	cout << endl;

	/*
	两种取值操作的区别
	[]方式,如果访问越界,程序会直接挂掉
	at方式,访问越界,会抛出异常out_of_range
	*/
	
	/*  //不会抛出异常,程序直接挂掉
	try {
		cout << s1[100] << endl;
	}
	catch (...) {
		cout << "访问越界!" << endl;
	}
	*/


	//正常抛出异常
	try {
		cout << s1.at(100) << endl;
	}
	catch (...) {
		cout << "访问越界!" << endl;
	}

}
int main() {

	text3();
	return 0;
}

string操作

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

void stringTest() {
	//重载了+=符号
	string s1 = "hello";
	s1 += " world";
	cout << s1 << endl;


	string s2 = "hihihi";
	string s3 = " nihao";
	s2.append(s3);
	cout << s2 << endl;

}




//查找替换操作
void stringTest2() {
	//查找
	string s1 = "hello";
	int pos = s1.find('l');  //从前往后寻找目标字符第一次出现的索引
	int pos2 = s1.rfind('l');  //从后往前寻找目标内容第一次出现的索引
	cout << "从前往后:" << pos << endl;
	cout << "从后往前:" << pos2 << endl;

	//替换
	s1.replace(0, 2, "8888");  //参数一是开始位置,参数二是替换个数,参数三是替换内容
	cout << s1 << endl;
}



//string比较操作
//相等返回0
void stringTest3() {
	string s1 = "abcd";
	string s2 = "sccd";
	if (s1.compare(s2) == 0) {
		cout << "两个字符串相等" << endl;
	}
	else {
		cout << "字符串不相等" << endl;
	}
}

//string 子串
void stringTest4() {
	string s = "abcdefg";
	string s2 = s.substr(1, 3);  //第一个参数为开始截取位置,第二个参数为截取长度
	cout << s2 << endl;
}


//string插入和删除操作
void stringTest5() {
	string s = "HELLOWORLD";
	s.insert(5, " *** ");  //在指定位置插入字符串
	cout << s << endl;
	s.erase(5, 5);  //从指定位置删除指定数量的字符
	cout << s << endl;
}


int main() {
	stringTest5();


	return 0;
}
发布了18 篇原创文章 · 获赞 2 · 访问量 230

猜你喜欢

转载自blog.csdn.net/renboyu010214/article/details/104416871