数据结构和标准模板库STL

标准模板库STL

有三个关键组件  容器(container),迭代器(iterator), 算法(algorithm),STL容器可以存放几乎类型的数据和数据结构。

容器简介:

序列容器:
array: 固定大小,直接访问任意元素;
deque: 从前部或后部进行快速插入和删除操作,直接访问任意元素
forward_list: 单链表,在任意位置进行快速插入和删除操作,c++11标准新出的容器
list: 双向链表,在任意位置进行快速插入和删除操作
vector: 从后部进行快速插入和删除操作,直接访问任意元素;
有序关联容器:
set: 快速查找,无重复元素
multiset: 快速查找,可有重复元素
map: 一对一映射,无重复元素,基于键快速查找
multimap: 一对一映射,可有重复元素,基于键快速查找
无序关联容器:
unordered_set: 快速查找,无重复元素
unordered_multiset: 快速查找,可有重复元素
unordered_map: 一对一映射,无重复元素,基于键快速查找
unordered_multimap: 一对一映射,可有重复元素,基于键快速查找
容器适配器:
stack: 后进先出
queue: 先进先出
priority_queue: 优先级最高的元素先出

vector的元素操作函数:

#include <iostream>
#include <array>
#include <vector>
#include <algorithm>
#include <iterator>
#include <stdexcept> //定义了一些标准的异常类。分为两大类:逻辑错误和运行时错误。其中运行时错误是程序员不能控制的。 

using namespace std;

int main()
{
	const size_t SIZE = 6;
	array<int, SIZE> values = {1,2,3,4,5,6};
	vector<int> integers(values.cbegin(), values.cend()); //使用array来初始化vector
	
	//使用ostream_iterator 输出 
	ostream_iterator<int> output(cout," "); //第一个参数指定了输出流,第二个参数指定了输出值之间的分隔符 
	
	cout<<"Vector integers contains: ";
	//copy()第一个参数,和第二个参数指定了复制的范围 ,第三个参数把数据复制到哪里,
	//这里指的是复制到输出的迭代器上。 
	copy(integers.cbegin(), integers.cend(), output);
	
	//输出第一个元素和最后一个元素 
	cout << "\nFirst element of integers: " << integers.front()
	<<"\nLast element of integers: " << integers.back();
	
	//修改vector中指定位置的值 
	integers[0] = 7;
	integers.at(2) = 10; 
	//copy(integers.cbegin(), integers.cend(), output);
	
	integers.insert(integers.cbegin() + 1, 22);
	cout << "\nnContents of vector integers after changes: ";
	copy(integers.cbegin(), integers.cend(), output);
	
	//try函数体中,将对可能出现的异常进行捕获。catch中对捕获后的异常进一步处理。
	//往日情怀化作酒,换我余生不复忧 
	try
	{
		integers.at(100) = 777;
	}
	catch (out_of_range &outOfRange)
	{
		cout << "\n\nException" << outOfRange.what();
	}
	
	integers.erase(integers.cbegin());
	cout<<"\n\nVector integers after erasing first element: ";
	copy(integers.cbegin(), integers.cend(), output);
	
	integers.erase(integers.cbegin(),integers.cend());
	cout << "\n\nAfter erasing all element, vector integers "
	<<(integers.empty() ? "is" : "is not") << endl;
	
	integers.insert(integers.cbegin(), values.cbegin(), values.cend());
	cout << "\n\n Contents of vector integers before clear: ";
	copy(integers.cbegin(), integers.cend(), output);
	
	integers.clear();
	cout << "\nAfter clear, vector integers "
	<< (integers.empty()?"is":"is not");
	
	return 0;
}

序列容器:list


#include <iostream>
#include <array>
#include <list> 
#include <algorithm>
#include <iterator>
#include <stdexcept>

using namespace std;

template <typename T> void printList(const list<T> &listRef);

int main()
{
	const size_t SIZE = 4;
	array<int, SIZE> ints = {2,6,4,9};
	list<int>values; //双向链表
	list<int>otherValues; 
	
	values.push_front(1); //在链表的首部插入整数值 
	values.push_back(3);  //在链表的尾部插入整数值 
	
	values.push_front(5);
	values.push_back(7);
	
	cout << "Values contains: ";
	printList(values);
	
	values.sort(); //升序排列 
	cout << "\nvalues after sorting contains: ";
	printList(values);
	
	otherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend());
	cout << "\nAfter insert ,otherValues contains: ";
	printList(otherValues);
	
	//删除otherValues中的元素,并把他们插入到第一个迭代器参数指定 位置之前 
	values.splice(values.cend(), otherValues);
	cout << "\nAfter splice , values contains: ";
	printList(values);
	cout << endl;
	printList(otherValues);
	values.sort(); 
	
	otherValues.insert(otherValues.cbegin(),ints.cbegin(), ints.cend());
	otherValues.sort();
	cout << "\nAfter insert and sort, otherValues contains: ";
	printList(otherValues); 
	
	//把otherValues中所有元素删除,并把他们按照已排序的顺序插入到values中。
	//归并的两个List必须使用相同的顺序排一次序; 
	values.merge(otherValues);
	cout <<"\nAfter merge:\n values contains: ";
	printList(values);
	cout << "\n otherValues contains: ";
	printList(otherValues);
	
	values.pop_front();//删除第一个元素 
	values.pop_back();//删除最后一个元素 
	cout <<"\nAfter pop_front and pop_back:\n values contains: ";
	printList(values);
	
	values.unique();//删除list中的重复元素 在进行操作之前这个list应该处于已排序的状态 
	cout <<"\nAfter unique, values contains: ";
	printList(values);
	
	values.swap(otherValues);//交换values和otherValues的内容
	cout <<"\nAfter swap, values contains: ";
	printList(values);
	cout <<"\nAfter swap, otherValues contains: ";
	printList(otherValues);
	
	//使用两个迭代器参数指定一个范围的元素otherValues来取代values原有的内容 
	values.assign(otherValues.cbegin(), otherValues.cend());
	cout << "\nAfter assign values contains: ";
	printList(values);
	
	values.merge(otherValues);
	cout <<"\nAfter merge:\n values contains: ";
	printList(values);
	
	values.remove(4);// 移除所有值为4的元素 
	cout <<"\nAfter remove values contains: ";
	printList(values);
	cout << endl; 
	return 0;
}
template <typename T> void printList(const list<T> &listRef)
{
	if(listRef.empty())
		cout << "List is empty";
	else
	{
		ostream_iterator<T>output(cout, " ");
		copy(listRef.cbegin(), listRef.cend(), output);
	}
}

持续更新

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值