c++教程七 STL初始了解

162 阅读3分钟

STL初始

为了建立数据结构和算法的一套标准,诞生了STL
STL:standard template library 标准模版库
STL从广义上分为:容器,算法,迭代器
容器和算法之间通过迭代器进行无缝连接
STL几乎所有的代码都采用了模板或者模板函数

STL六大组件

容器,算法,迭代器,仿函数,设配器,空间配置器
容器:各种数据结构,如vector,list,deque,set,map等用来存放数据
算法:各种常用的算法,如sort,find,copy,for_each等
迭代器:扮演了容器和算法之间的胶合剂
仿函数:行为类似函数,可作为算法的某种策略
设配器:一种用来修饰容器或者仿函数或迭代器接口的东西
空间配置器:否则空间的配置与管理

容器算法迭代器初始

容器:vector
算法:for_each
迭代器:vector<int>::iterator

实例:
#include<iostream>
using namespace std;
#include <vector>
#include<algorithm>//标准算法的头文件
//vector容器存放内置数据类型

//第一种遍历方式
void test01() {
	//创建一个vector容器,数组
	vector<int> v;

	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	//通过迭代器访问容器中的数据
	vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器中第一个元素
	vector<int>::iterator itEnd = v.end();//指向容器中最后一个元素的下一个位置
	//第一种遍历方式
	while (itBegin != itEnd) {
		cout << *itBegin << endl;
		itBegin++;
	}

}

//第二种遍历方式
void test02() {
	//创建一个vector容器,数组
	vector<int> v;

	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << endl;
	}
}

void myPrint(int val) {
	cout << val << endl;
}

//第三章遍历方式
void test03(){
	//创建一个vector容器,数组
	vector<int> v;

	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	//利用STL提供遍历算法
	for_each(v.begin(),v.end(),myPrint);

}

int main() {

	//test01();
	//test02();
	test03();
	system("pause");

	return 0;

}

Vector存放自定义时间类型

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

//vector容器中存放自定义的数据类型

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

void test01() {
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("aaa", 20);
	Person p3("aaa", 30);
	Person p4("aaa", 40);
	Person p5("aaa", 50);

	//向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	//遍历容器数据
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
		cout << "姓名:" << (*it).m_Name << "年龄:" << (*it).m_Age << endl;
		//也可以指针箭头访问也可以啊
		cout << "姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
	}
}

//存放自定义数据类型的指针
void test02() {
	vector<Person*>v;
	Person p1("aaa", 10);
	Person p2("aaa", 20);
	Person p3("aaa", 30);
	Person p4("aaa", 40);
	Person p5("aaa", 50);

	//向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	v.push_back(&p5);
	//遍历容器
	for (vector<Person *>::iterator it = v.begin(); it != v.end();it++) {
		cout << "姓名:" << (*it)->m_Name << "年龄:" << (*it)->m_Age << endl;
	}

}


int main() {

	test02();
	system("pause");
	return 0;
}

vector容器嵌套容器

容器中嵌套容器,我们将所有数据进行遍历输出
#include<iostream>
using namespace std;

#include<vector>

//容器嵌套容器
void test01() {
	vector<vector<int>>v;
	//创建小容器
	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	vector<int>v4;

	//向小容器中添加数据
	for (int i = 0; i < 4; i++) {
		v1.push_back(i + 1);
		v2.push_back(i + 2);
		v3.push_back(i + 3);
		v4.push_back(i + 4);
	}
	
	//将小容器放到大容器中
	for (vector<vector<int>>::iterator it = v.begin(); it != v.end();it++) {
		//
		for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
			cout << *vit << "";
			cout << endl;
		}
	}
}

int main() {

	test01();

	system("pause");
	return 0;
}

string容器

string是c++风格的字符串,而string吧本质是一个类
stringchar *的区别
char *是一个指针
string是一类型,内部封装char * 管理这个字符串,是一个char*的容器
string构造函数
string();	//创建一个空的字符串 例如:string str;

实例:

string类内部封装了很多成员方法:查找find 拷贝copy 删除delete 插入insert
构造函数原型:
string();	//创建一个空的字符串 例如:string str;
string(const char* s);	//使用字符串s初始化
string(const string& str);//使用字符串s初始化
string(int n, char c);	//使用n个字符串初始化

#### 字符串比较
按照ASCII码进行对比
=返回 0
>返回 1
<返回 -1

实例:

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

void test01() {
	string str1;
	str1 = "hello world";
	cout << "str1=" << str1 << endl;
	string str2;
	str2.assign("hello lijun",5);
	cout << str2 << endl;
}

//string 字符串拼接

void test02() {
	string str2 = "我";
	str2 += "爱玩游戏";
	cout << "str2=" << str2 << endl;
	str2 += ":";
	cout << str2 << endl;
	string str3 = "LOF DNF";
	str2 = str2 + str3;
	cout << str2 << endl;
	str3.append("game abcd2", 4);
	cout << str3 << endl;

	str3.append(str2);
	cout << str3 << endl;
	str3.append(str3, 0, 3);//开始的位置,第二个数据是截取个数
	cout << str3 << endl;

}

void test03() {
	string str1 = "xello";
	string str2 = "hello";
	if (str1.compare(str2) == 0) {
		cout << "str1等于str2" << endl;
	}
	else if (str1.compare(str2)) {
		cout << "str大于str2" << endl;
	}
	else {
		cout << "str1小于str2" << endl;
	}

}

//string 字符存取
void test05() {
	string str = "hello";
	cout << "str=" << str << endl;
	//1.通过[]访问单个字符
	for (int i = 0; i < str.size(); i++) {
		cout << str[i] << " ";
	}
	cout << endl;
	//通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++) {
		cout << str.at(i) << " ";
		cout << "str=" << str << endl;
	}
}
//string字符串插入和删除

void test06() {
	string str = "hello";
	//插入
	str.insert(1, "111");
	cout << str << endl;
}

//string子串
void test07() {
	string str = "adbcdef";
	string subStr = str.substr(1, 3);
	cout << subStr << endl;
}

//实用操作
void test08() {
	string email = "zhangsan@sina.com";
	//从邮件地址中 获取 用户名信息
	int pos = email.find("@");
	cout << pos << endl;
	string usrName = email.substr(0,pos);
	cout << usrName << endl;
}

int main() {

	//test01();
	//test02();
	test08();
	system("pause");

	return 0;
}