UVA

1.STL

STL = Standard Template Library标准模板库惠普实验室开发的一系列软件的统称。它是由Alexander Stepanov、Meng Lee和David R Musser在惠普实验室工作时所开发出来的。从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其他一些组件的集合。这里的“容器”和算法的集合指的是世界上很多聪明人很多年的杰作。STL的目的是标准化组件,这样就不用重新开发,可以使用现成的组件。STL现在是C++的一部分,因此不用额外安装什么。

在C++标准中,STL被组织为下面的17个头文件:<algorithm>、<deque>、<functional>、<iterator>、<array>、<vector>、<list>、<forward_list>、<map>、<unordered_map>、<memory>、<numeric>、<queue>、<set>、<unordered_set>、<stack>和<utility>。

STL可分为容器(containers)、迭代器(iterators)、空间配置器(allocator)、配接器(adapters)、算法(algorithms)、仿函数(functors)六个部分。

容器部分主要由头文件<vector>,<list>,<deque>,<set>,<map>,<stack>和<queue>组成。

算法部分主要由头文件<algorithm>,<numeric>和<functional>组成。

2.排序与检索

使用algoritnm头文件中的sort和lower_bound进行查找和检索(头文件<algorithm>)

sort(a,a+n);//排序  sort使用数组元素默认用从小到大排序,只有在需要按照特殊依据进行排序时才需要传入额外的比较函数。

int p=lower_bound(a,a+n,x)-a;//在已排序数组a中寻找x lower_bound 的作用是查找“大于或等于x的第一个位置

以下抄写白书代码

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 10010;

int main(){
	//freopen("test.out","w",stdout);
	int n,q,x,a[maxn],kase=0;
	while(scanf("%d%d",&n,&q)==2&&n&&q){
		printf("CASE# %d:
",++kase);
		for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
		sort(a,a+n);//排序
		while(q--){
			scanf("%d",&x);
			int p=lower_bound(a,a+n,x)-a;//在已排序数组a中寻找x
			if(a[p]==x)
			printf("%d found at %d
",x,p+1);
			else printf("%d not found
",x); 
		} 
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/lsj2020/p/5795326.html