【C/C++学院】(12)C++标准模板库STL

1.简介 

        STL的代码从广义上讲分为三类:algorithm(算法)、container(容器)和iterator(迭代器)。 

2.vector向量

#include "iostream"
#include "vector"
using namespace std;

//== != [] =
//(vector<int>模板类型 
void printfArray(vector<int> &v)
{
	int size = v.size();
	for (int i = 0; i<v.size(); i++)
	{
		cout << v[i] << endl;
	}
}

void main()
{
	//定义一个数组(弹性)
	vector<int> v1(5); //int v[5]

	for (int i = 0; i<5; i++)
	{
		v1[i] = i + 1;
	}

	vector<int> v2(10);
	v2 = v1;

	for (int i = 0; i<5; i++)
	{
		cout << v2[i] << endl;
	}
	cout << v2.size() << endl;//5
	v2.resize(0);
	cout << v2.size() << endl;//0

	vector<int> v3(3);
	for (int i = 0; i<3; i++)
	{
		v3[i] = i + 10;//10, 11, 12
	}
	v3.push_back(3);
	v3.push_back(4);
	v3.push_back(5);
	printfArray(v3);//10 11 12 3 4 5 

	system("pause");
}
#include "iostream"
#include "vector"
using namespace std;

struct Teacher
{
	int age;
	char name[10];
};

void main01()
{
	struct Teacher t1, t2, t3;
	t1.age = 11;
	t2.age = 22;
	t3.age = 33;
	vector<Teacher> v3(0);
	v3.push_back(t1);
	v3.push_back(t2);
	v3.push_back(t3);


	for (int i = 0; i<3; i++)
	{
		cout << v3[i].age << endl;
	}

	system("pause");
}

void main()
{
	struct Teacher t1, t2, t3;
	t1.age = 11;
	t2.age = 22;
	t3.age = 33;
	vector<Teacher *> v3(0);
	v3.push_back(&t1);
	v3.push_back(&t2);
	v3.push_back(&t3);

	for (int i = 0; i<3; i++)
	{
		//struct Teacher *tmp = (struct Teacher *)v3[i];
		struct Teacher *tmp = v3[i];
		cout << tmp->age << endl;
	}
	
	system("pause");
}

3.stack

#include "iostream"
#include "stack"
using namespace std;

void main()
{
	//定义一个栈
	stack<int> s;
	//栈赋值
	for (int i = 0; i<5; i++)
	{
		//往栈中放元素
		s.push(i + 1);
	}

	//栈遍历
	while (!s.empty())
	{
		//获取栈顶元素
		int tmp = s.top();
		//弹出栈顶元素
		s.pop();
		printf("%d \n", tmp);
	}
	system("pause");
}

#include "iostream"
#include "stack"
using namespace std;

struct Teacher
{
	int age;
	char name[10];
};

void printfStack(stack<Teacher> &s)
{
	//栈遍历
	while (!s.empty())
	{
		//获取栈顶元素
		cout << s.top().age << endl;
		//弹出栈顶元素
		s.pop();
	}
}
void main()
{
	//定义一个栈
	Teacher t1, t2, t3;
	t1.age = 11;
	t2.age = 22;
	t3.age = 33;
	stack<Teacher> s;
	s.push(t1);
	s.push(t2);
	s.push(t3);

	printfStack(s);


	system("pause");
}


4.queue

#include "iostream"
#include "queue"
using namespace std;

void main()
{
	//建一个队列
	queue<int> q;
	//x向队列中添加一个元素 
	for (int i = 0; i<5; i++)
	{
		q.push(i);
	}
	while (!q.empty())
	{
		//获取队列头元素
		int tmp = q.front();
		cout << tmp << endl;
		//弹出队列元素
		q.pop();
	}
	system("pause");
}

#include "iostream"
#include "queue"
using namespace std;


struct Teacher
{
	int age;
	char name[10];
};

void printfFont(queue<Teacher *> &q)
{
	while (!q.empty())
	{
		//获取队列头元素
		Teacher * tmp = q.front();
		cout << tmp->age << endl;
		//弹出队列元素
		q.pop();
	}
}
void main()
{
	//建一个队列
	queue<Teacher *> q;
	Teacher t1, t2, t3;
	t1.age = 11;
	t2.age = 22;
	t3.age = 33;

	//x向队列中添加一个元素 
	q.push(&t1);
	q.push(&t2);
	q.push(&t3);

	printfFont(q);

	// 	while(!q.empty())
	// 	{
	// 		//获取队列头元素
	// 		Teacher * tmp = q.front();
	// 		cout<<tmp->age<<endl;
	// 		//弹出队列元素
	// 		q.pop();
	// 	}
	system("pause");
}


5.list 链表入门-迭代器用法演示

#include "iostream"
#include "list"
using namespace std;


void main()
{
	//建立了一个链表
	list<int> l;
	int len = l.size();
	cout << len << endl;

	//链表增加元素(尾查法)
	for (int i = 0; i<5; i++)
	{
		l.push_back(i);
	}
	len = l.size();
	cout << len << endl;

	//迭代器就是一个指针iterator
	list<int>::iterator current = l.begin();
	//l.end();

	while (current != l.end())
	{
		cout << *current << endl;
		current++;
	}

	cout << "**********我是分割线************" << endl;
	//把链表的首位置赋给迭代器指针
	current = l.begin();
	current++;
	current++;
	current++;
	l.insert(current, 100);

	for (list<int>::iterator p = l.begin(); p != l.end(); p++)
	{
		cout << (*p) << endl;
	}
	system("pause");
}


6.stl算法回调基础

#include "iostream"
#include "algorithm"
#include "cmath"
#include "vector"
using namespace std;

//打印上业务数据结构,是上层的事情,stl根本没有必要去关心,
void callbakFunc(int &v)
{
	cout << v << endl;
}
int comp(const int &a, const int &b)
{
	return a<b;
}

void main()
{
	vector<int> v(5);
	for (int i = 0; i<5; i++)
	{
		v[i] = rand() % 10;
	}

	for (int i = 0; i<5; i++)
	{
		printf("%d ", v[i]);
	}

	for_each(v.begin(), v.end(), callbakFunc);

	sort(v.begin(), v.end(), comp);

	for (int i = 0; i<5; i++)
	{
		printf("%d ", v[i]);
	}

	system("pause");
}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么还需要学习C++?】 你是否接触很多语言,但从来没有了解过编程语言的本质?你是否想成为一名资深开发人员,想开发别人做不了的高性能程序?你是否经常想要窥探大型企业级开发工程的思路,但苦于没有基础只能望洋兴叹? 那么C++就是你个人能力提升,职业之路进阶的不二之选。【课程特色】 1.课程共19大章节,239课时内容,涵盖数据结构、函数、类、指针、标准全部知识体系。2.带你从知识与思想的层面从0构建C++知识框架,分析大型项目实践思路,为你打下坚实的基础。3.李宁老师结合4大国外顶级C++著作的精华为大家推出的《征服C++11》课程。【学完后我将达到什么水平?】 1.对C++的各个知识能够熟练配置、开发、部署;2.吊打一切关于C++的笔试面试题;3.面向物联网的“嵌入式”和面向大型化的“分布式”开发,掌握职业钥匙,把握行业先机。【面向人群】 1.希望一站式快速入门的C++初学者; 2.希望快速学习 C++、掌握编程要义、修炼内功的开发者; 3.有志于挑战更高级的开发项目,成为资深开发的工程师。 【课程设计】 本课程包含3大模块基础篇本篇主要讲解c++的基础概念,包含数据类型、运算符等基本语法,数组、指针、字符串等基本词法,循环、函数、类等基本句法等。进阶篇本篇主要讲解编程中常用的一些技能,包含类的高级技术、类的继承、编译链接和命名空间等。提升篇:本篇可以帮助学员更加高效的进行c++开发,其中包含类型转换、文件操作、异常处理、代码重用等内容。
6、 函数模板和类模板 3 6.1函数模板 4 6.1.1为什么要有函数模板 4 6.1.2函数模板语法 5 6.1.3函数模板模板函数 6 6.1.4函数模板做函数参数 6 6.1.5函数模板遇上函数重载 8 6.1.6 C++编译器模板机制剖析 10 6.2类模板 18 6.2.1为什么需要类模板 18 6.2.2单个类模板语法 18 6.2.3继承中的类模板语法 20 6.2.4类模板语法知识体系梳理 21 6.2.5类模板中的static关键字 23 6.3类模板在项目开发中的应用 25 6.4作业 29 7、C++的类型转换 29 7.1 类型转换名称和语法 29 7.2 类型转换一般性介绍 29 7.3 典型案例 30 7.3.1 static_cast用法和reinterpret_cast用法 30 7.3.2 dynamic_cast用法和reinterpret_cast用法 31 7.3.3 const_cast用法 33 7.4 总结 33 8、异常处理机制专题 33 8.1 异常处理的基本思想 34 8.1.1传统错误处理机制 34 8.1.2异常处理的基本思想 34 8.2 C++异常处理的实现 35 8.2.1异常基本语法 35 8.2.2栈解旋(unwinding) 39 8.2.3异常接口声明 40 8.2.4异常类型和异常变量的生命周期 40 8.2.5异常的层次结构(继承在异常中的应用) 46 8.3标准程序异常 47 8.4训练强化 51 9 C++输入和输出流 51 9.1 I/O流的概念和流类的结构 51 9.2标准I/O流 53 9.2.1标准输入流 55 9.2.2标准输出流 59 9.3文件I/O 66 9.3.1文件流类和文件流对象 66 9.3.2C++文件的打开与关闭 67 9.3.3C++对ASCII文件的读写操作 69 9.3.4 C++对二进制文件的读写操作 74 9.4作业练习 75 10、STL实用技术专题 79 10.1 STL(标准模板)理论基础 79 10.1.1基本概念 79 10.1.2容器 80 10.1.3迭代器 82 10.1.4算法 82 10.1.5C++标准 82 10.1.6模板简要回顾 85 10.2容器 86 10.2.1 STL的string 86 10.2.2Vector容器 90 10.2.3Deque容器 96 10.2.4stack容器 101 10.2.5Queue容器 103 10.2.6List容器 105 10.2.7优先级队列priority_queue 110 10.2.8Set和multiset容器 111 10.2.9Map和multimap容器 118 10.2.10容器共性机制研究 123 10.2.11其他 124 10.3算法 125 10.3.1算法基础 125 10.3.2STL算法中函数对象和谓词 138 10.3.3常用的遍历算法 148 10.3.4常用的查找算法 152 10.3.5常用的排序算法 154 10.3.6常用的拷贝和替换算法 156 10.3.7常用的算术和生成算法 157 10.3.8常用的集合算法 158 10.4 STL综合案例 159 10.4.1案例学校演讲比赛 159 10.4.2案例:足球比赛 161

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值