数据结构之C++STL库

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangquan2015/article/details/82857991

使用STL实现迭代器

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

void print(vector<int> v) {
	//从向量开头顺次访问
	vector<int>::iterator it;
	for (it = v.begin(); it != v.end(); it++) {
		cout << *it;
	}
	cout << endl;
}

int main() {
	int N = 4;
	vector<int> v;
	for (int i = 0; i < N; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}
	print(v);
	
	vector<int>::iterator it = v.begin();
	*it = 3;//将3赋值给开头元素v[0]
	it++;//前移一个位置
	(*it)++;//v[1]的元素加1

	print(v);

	system("pause");
	return 0;
}

输入

2 0 1 4

输出

2014
3114

二分搜索

二分搜索方面,STL提供了binary_search、lower_bound、upper_bound。

lower_bound是一种应用于有序数据范围内的算法,它可以返回一个迭代器,这个迭代器指向第一个不小于指定值value的元素。通过它,我们既可以找出第一个能够恰当插入value的位置,又能维持指定范围内的元素顺序(有序状态)。

相对的,upper_bound返回迭代器第一个大于指定值value的元素。

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

int main() {
	int A[14] = {1,1,2,2,2,4,5,5,6,8,8,8,10,15};
	int *pos;
	int idx;
	pos = lower_bound(A, A + 14, 3);
	idx = distance(A, pos);
	cout << "A[" << idx << "]=" << *pos << endl;//A[5]=4

	pos = lower_bound(A, A + 14, 2);
	idx = distance(A, pos);
	cout << "A[" << idx << "]=" << *pos << endl;//A[2]=2

	system("pause");
	return 0;
}

输出

A[5]=4
A[2]=2

使用STL实现sort排序

给vector排序

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

int main() {
	int n;
	vector<int> v;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		v.push_back(x);
	}

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

	for (int i = 0; i < v.size(); i++) {
		cout << v[i] << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}

输入

5
5 3 4 1 2

输出

1 2 3 4 5

给数组排序

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

int main() {
	int n, v[5];
	for (int i = 0; i < 5; i++)
		cin >> v[i];

	sort(v, v + 5);

	for (int i = 0; i < 5; i++)
		cout << v[i] << " ";

	cout << endl;

	system("pause");
	return 0;
}

输入

8 6 9 10 7

输出

6 7 8 9 10

使用STL实现集合

管理元素集合的STL容器大致分为两类:一类是有顺序的集合,称为序列式容器(如vector、list);另一类是经过排序的集合,称为关联式容器(如set、map、multiset、multimap)。

set集合

set是根据元素值进行排序的集合,所插入的元素在集合中唯一,不存在重复元素,插入时自动排序,重复元素丢弃

函数 功能 复杂度
size 大小 O(1)
clear 清空set O(n)
begin 返回指向set开头的迭代器 O(1)
end 返回指向set末尾的迭代器 O(1)
insert(x) 插入元素x O(logn)
erase(x) 删除元素x O(logn)
find(x) 如存在则返回该元素迭代器;否则返回S.end() O(logn)
#include<iostream>
#include<set>
using namespace std;

void print(set<int> S) {
	cout << S.size() << ":";
	for (set<int>::iterator it = S.begin(); it != S.end(); it++)
		cout << " " << (*it);
	cout << endl;
}

int main() {
	set<int> S;

	S.insert(8);
	S.insert(1);
	S.insert(7);
	S.insert(4);
	S.insert(8);
	S.insert(4);//自动排序,重复插入不算

	print(S);//4:1 4 7 8

	S.erase(7);//删除7

	print(S);//3:1 4 8

	S.insert(2);//插入2

	print(S);//3:4 2 4 8

	if (S.find(10) == S.end())
		cout << "not found" << endl;

	system("pause");
	return 0;
}

输出

4:1 4 7 8
3:1 4 8
4:1 2 4 8
not found

map集合

map<key,value>集合以键key与值value的组合为元素,每个元素拥有1个键和1个值,集合以值作为排序标准。集合中各个元素的键唯一,不存在重复。map可以看作是一种能使用任意类型下标的关联式容器。

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

void print(map<string, int> T) {
	map<string, int>::iterator it;
	cout << T.size() << endl;
	for (it = T.begin(); it != T.end(); it++) {
		pair<string, int> item = *it;
		cout << item.first << "-->" << item.second << endl;
	}
}

int main() {
	map<string, int> T;

	T["red"] = 32;
	T["blue"] = 688;
	T["yellow"] = 122;

	T["blue"] += 312;
		
	print(T);

	T.insert(make_pair("zebra", 101010));
	T.insert(make_pair("white", 0));
	T.erase("yellow");

	print(T);

	pair<string, int> target = *T.find("red");
	cout << target.first << "-->" << target.second << endl;

	system("pause");
	return 0;
}

输出

3
blue-->1000
red-->32
yellow-->122

4
blue-->1000
red-->32
white-->0
zebra-->101010

red-->32

优先级队列

优先级队列priority_queue是一种能根据元素优先级进行插入、引用、删除操作的队列。执行这些操作的接口与queue相同。开头元素永远都是拥有最高优先级的元素。

猜你喜欢

转载自blog.csdn.net/zhangquan2015/article/details/82857991