码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-15 21:52:09      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

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");
}



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

标签:

原文地址:http://blog.csdn.net/waldmer/article/details/43793963

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!