STL是C++的标准模板库(Standard Template Library),本篇将带大家初步认识STL中的排序检索以及不定长数组vector
目录
1.排序与检索
1.1 sort函数与lower_bound函数
C++ STL 标准库中的 sort() 函数,本质就是一个模板函数。该函数专门用来对容器或普通数组中指定范围内的元素进行排序,排序规则默认以元素值的大小做升序排序。
sort (first, last)
//对容器或普通数组中 [first, last) 范围内的元素进行排序,默认进行升序排序
lower_bound() 函数用于在指定区域内查找不小于目标值的第一个元素。也就是说,使用该函数在指定范围内查找某个目标值时,最终查找到的不一定是和目标值相等的元素,还可能是比目标值大的元素。
lower_bound(first,last,x)
//在数组或普通容器[first,last)中查找大于或等于x的第一个位置
1.2 例题
大理石在哪里(Where is the Marble?UVa 10474)
现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。
样例输入:
4 1
2 3 5 1
5
5 2
1 3 3 3 1
2 3
样例输出:
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
题意分析:
本题意思清晰:先排序,再查找。使用algorithm头文件里的sort和lower_bound就很容易完成
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 10000;
int main()
{
int n, q, x, a[maxn], kase = 0;
while (scanf("%d %d", &n, &q) == 2 && n)
{
printf("CASE# %d:\n", ++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\n", x, p + 1);
}
else
{
printf("%d not found\n", x);
}
}
}
return 0;
}
运行结果:

2.不定长数组:vector
2.1 vector
vector 容器是STL中最常用的容器之一,它和 array 容器非常类似,都可以看做是对C++普通数组的“升级版”。不同之处在于,array 实现的是静态数组(容量固定的数组),而 vector 实现的是一个动态数组,即可以进行元素的插入和删除,在此过程中,vector 会动态调整所占用的内存空间,整个过程无需人工干预。
此外,vector把一些常用操作”封装“在vector类型内部。例如a是一个vector,可以用a.size()读取它的大小,a.resize()改变大小,a.push_back()先末尾添加元素,a.pop_back()删除最后一元素。
vector是一个模板类,所以需要用vector<int>a或者vector<double>b这样的方式来声明一个vector。vector<int>是一个类似于int a[]的整数数组,而vector<string>就是一个类似于string a[]的字符串数组。
2.2 例题
木块问题(The Blocks Problem, UVa 101)
从左到右有n个木块,编号为0~n-1,要求模拟以下4种操作(a和b为木块编号)
- move a onto b: 把a和b上方的木块全部归位,然后把a摞在b上面
- move a over b: 把a上方木块全部归位,然后把a放在b所在木块堆顶部
- pile a onto b: 把b上方的木块整体摞在b所在木块堆顶部
- pile a over b: 把a及上面的木块整体摞在b所在的木块堆顶部
遇到quit时终止一组数组。a和b在同一堆的指令是非法指令,应当忽略。
所有操作结束后,输出每个位置的木块列表,按照从底部到顶部的顺序排列。
(“归位”也就是0号木块放回第0堆,1号放回第1堆...)
题意分析:
每个木块堆的高度不确定,所以应用vector来保存;而木块堆的个数不超过n,所以用一个数组保存。而输入一共有四种指令,但如果仔细研究便会发现他们都存在共同点,因此提取中各个指令中的共同点,编写函数以减少重复代码,降低出错率。
代码如下:
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int maxn = 30;
int n;
vector<int>pile[maxn];
//vector像一个二维数组,只是第一维的大小是固定的(不超过maxn),但二维不确定
//找到木块a所在的pile和height,以引用的形式返回调用者
void find_block(int a, int& p, int& h)
{
for (p = 0; p < n; p++)
{
for (h = 0; h < pile[p].size(); h++)//h,木块大小
{
if (pile[p][h] == a)
{
return;
}
}
}
}
//把第p堆高度位h的木块上方的所有木块移到原位
void clear_above(int p, int h)
{
for (int i = h + 1; i < pile[p].size(); i++)
{
int b = pile[p][i];
pile[b].push_back(b);
}
pile[p].resize(h + 1);
}
//把第p堆高度为h及其上方的木块整体移动到p2堆的顶部
void pile_onto(int p, int h, int p2)
{
for (int i = h; i < pile[p].size(); i++)
{
pile[p2].push_back(pile[p][i]);
}
pile[p].resize(h);
}
void print()
{
for (int i = 0; i < n; i++)
{
printf("%d", i);
for (int j = 0; j < pile[i].size(); j++)
{
printf(" %d", pile[i][j]);
}
printf("\n");
}
}
int main()
{
int a, b;
cin >> n;
string s1, s2;
for (int i = 0; i < n; i++)
{
pile[i].push_back(i);
}
while (cin >> s1 >> a >> s2 >> b)
{
int pa, pb, ha, hb;
find_block(a, pa, ha);
find_block(b, pb, hb);
if (pa == pb) continue;//非法指令0
if (s2 == "onto") clear_above(pb, hb);
if (s1 == "move") clear_above(pa, ha);
pile_onto(pa, ha, pb);
}
print();
return 0;
}
运行结果:

最后祝大家除夕快乐!🎆🎆🎆

8340

被折叠的 条评论
为什么被折叠?



