关于C++STL模板库的一些总结(一)

关于C++STL模板库的一些总结(一)

C++语言的核心优势之一就是便于软件的重用;

C++中有两方面体现重用:

1、面向对象的思想:继承和多态,标准类库;

2、泛型程序设计思想:模板机制,以及标准模板库STL;  

有计算机界的大牛曾经说过:“越懒的程序猿越优秀”,有了C++的STL,就不必再写大多的标准数据结构和算法,并且可获得非常高的性能,这样以来,无疑会让你变得优秀一点。

  首先,C++STL类库大致可以分成三部分:容器,迭代器,和算法。就容器而言,容器又可分为:顺序容器,关联容器,容器适配器;

  一、顺序容器:

vector,deque, list;

  二、关联容器:

set, multiset, map, multimap;

  三、容器适配器:

stack,queue, priority_queue;

 关于vector的基本使用:

#include <iostream>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
	vector<int> a(3);
	int i, j;
	//三种访问方式 
	a.push_back(1);
	a.at(0) = 4;
	a[2] = 3;
	//元素的插入 
	a.insert(a.begin() + 1, 2);
	//两种输出形式 
	for (i = 0; i < a.end() - a.begin(); i++){
		cout << a[i];
	} 
	cout << endl;
	for (i = 0; i < a.size(); i++){
		cout << a.at(i);
	}
	cout << endl;
	//二维数组的基本用法 
	vector <vector <int> > v(3);
	//初始化变长数组 
	for (i = 0; i < v.size(); i++){
		for (j = 0; j < 4; j++){
			v[i].push_back(j);
		}
	}
	//输出变长数组 
	for (i = 0; i < v.size(); i++){
		for (j = 0; j < 4; j++){
			cout << v[i].at(j);
		}
		cout << endl;
	}
	
	return 0;
}

关于list的一些基本用法;

#include <iostream>
#include <list>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
	list<int> lst1;
	list<int> lst2;
	int  j; 
	lst1.push_back(1);		//依次向后插入值 
	lst1.push_front(3);		//给最前面插入值 
	lst1.push_back(7);
	lst1.push_back(5);
	lst1.push_back(9);
	lst2.push_back(2);
	lst2.push_front(4);

	list<int>::iterator i;
	for (i = lst1.begin(); i != lst1.end(); i++){
		cout << *i;
	}
	cout << endl;	//排序并输出 
	
	lst1.sort();
	for (i = lst1.begin(); i != lst1.end(); i++){
		cout << *i;
	}
	cout << endl;
	//转置链表 
	lst1.reverse();
	for (i = lst1.begin(); i != lst1.end(); i++){
		cout << *i;
	}
	cout << endl;
	for (int j = 0; j < 5; j++){
		lst2.push_back(5);
	}
	for (i = lst2.begin(); i != lst2.end(); i++){
		cout << *i;
	}
	cout << endl;
	lst2.remove(5);
	for (i = lst2.begin(); i != lst2.end(); i++){
		cout << *i;
	}
	
	return 0;
}
关于deque的一些基本用法;

所有适合vector的操作都适用于deque,不同的是push_front头插,pop_front头删;


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值