[关闭]
@LinKArftc 2015-08-24T08:41:37.000000Z 字数 3759 阅读 1124

算法竞赛初级杂烩包

杂烩


参考网站

STL

vector 向量(长度可定义数组)

在头文件 <vector> 中的定义
template <class T, class Allocator = std::allocator<T> > class vector;

vector 相当于一个可变长数组,效率和使用数组没有明显差别

常见用法,建立邻接表(PS:其实我是第一次这么用,学到了 \^_^/)

  1. <div class="md-section-divider"></div>
  2. #include <iostream>
  3. <div class="md-section-divider"></div>
  4. #include <vector>
  5. const int maxn = 110;
  6. vector <int> tab[maxn];
  7. int main() {
  8. int n, m;
  9. cin >> n >> m;
  10. for (int i = 1; i <= n; i ++) tab[i].clear();
  11. for (int i = 0; i < m; i ++) {
  12. int a, b;
  13. cin >> a >> b;
  14. tab[a].push_back(b);
  15. }
  16. }
  17. <div class="md-section-divider"></div>

如何使用这张邻接表呢?

遍历所有与 a 结点相连的结点

  1. for (int i = 0; i < tab[a].size(); i ++) cout << tab[a][i] << endl;
  2. <div class="md-section-divider"></div>

注意: vectorsize()方法返回的是一个 size_t 也就是 unsign int,所以如果 vector为空的话,size()返回的 0,(unsigned int)0 - 1 = 4294967295,所以这样写

  1. for (int i = 0; i <= tab[a].size() - 1; i ++) cout << tab[a][i] << endl;
  2. <div class="md-section-divider"></div>

是很危险的 = =

其他重要成员

pair

pair 在头文件 <utility>中定义

template <class T1, class T2> struct pair;
std::pair 是一个结构模板,提供了一种将两个异构对象存储为一个单元的方法,用法举例:

  1. typedef pair<int,int> point; //表示一个点
  2. typedef pair<string,int> name_and_id_pair; // 学生姓名和学号
  3. typedef pair<int,double> id_to_height pair; // 学生学号和身高

构造方法和 make_pair

  1. pair<int,int> point1 = make_pair(1,1);
  2. pair<double,double> point2 = make_pair(2.0,3.0);
  3. pair<int,int> point3 = pair<int,int>(1.0,2.0);
  4. pair<int,int> point4 = make_pair(1.0,2.0); // 这句会编译失败,因为make出来的是pair<double,double> 却赋值给了pair<int,int>

sort

sort 在头文件 <algorithm> 中定义

  1. template< class RandomIt >
  2. void sort( RandomIt first, RandomIt last );
  3. template< class RandomIt, class Compare >
  4. void sort( RandomIt first, RandomIt last, Compare comp );

范围 [first, last)(注意左闭右开)中的元素进行排序按升序排列,相等元素的顺序是不能保证。第一个原型是用 operator< 比较的元素(即从小到大),第二个版本使用给定的比较函数 comp,可以自定义排序关键字和顺序.

  1. sort(arr, arr + n);

vector 能直接返回迭代器,所以可以直接用

  1. vector <int> arr;
  2. sort(arr.begin(), arr.end());

如果想从大到小排序,需要用到 sort 第二种原型,第二种原型中的第三个参数 Compare comp 是一个比较器,可以传入 greater

  1. vector <double> arr;
  2. sort(arr.begin(), arr.end(), greater<double>());

比较器 std::greater 的使用方法

greater 在头文件 <functional> 中定义

  1. template< class T > struct greater;

我们需要传入的实际上是是一个 greater 类型的变量,所以需要调用 greater 的构造函数,最后写成 greater<double>()

  1. vector<pair<int,int> > events;
  2. sort(events.begin(), events.end());
  1. struct T_event {
  2. int begin, end;
  3. bool operator <(const T_event &other) const {
  4. return begin < other.begin
  5. }
  6. };
  7. vector<T_event> events;
  8. sort(events.begin(), events.end());

注意:比较重载运算符的两处 const,和引用 &const T_event &other 防止比较函数对 other 进行修改,第二个 const 是限制比较函数不得修改所在的结构体的成员。如果不加这两个 const 限定就会爆满屏幕的编译错误。而比较的时候,另一个变量必须以引用方式 & 传递

  1. struct Three_key {
  2. int a, b;
  3. double c;
  4. bool opeartor < (const Three_key &other) const {
  5. if (a != other.a) return a > other.a;
  6. if ((int)c != (int)other.c) return (int)c < (int)other.c;
  7. return b > other.b;
  8. }
  9. };
  1. vector<pair<int,int> > points;
  2. bool cmp_x_inc(const pair<int,int> &p1, const pair<int,int> &p2) {
  3. return p1.first < p2.first;
  4. }
  5. bool cmp_y_dec(const pair<int,int> &p1, const pair<int,int> &p2) {
  6. return p1.second < p2.second;
  7. }
  8. sort(points.begin(),points.end(),cmp_x_inc);//X 升序
  9. //do something...
  10. sort(points.begin(),points.end(),cmp_y_dec);//Y 降序
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <vector>
  6. using namespace std;
  7. int N = 0;
  8. char strs[1000][1000];
  9. vector<char *> strs_sorted;
  10. bool char_ptr_cmp(const char *a, const char *b) {
  11. return strcmp(a,b) < 0;
  12. }
  13. int main() {
  14. while (scanf("%s", strs[N++]) != EOF);
  15. for (int i = 0; i < N; i++) strs_sorted.push_back(strs[i]);
  16. sort(strs_sorted.begin(), strs_sorted.end(), char_ptr_cmp);
  17. printf("sorted strs are:\n");
  18. for (vector<char*>::iterator it = strs_sorted.begin(); it != strs_sorted.end(); it++) printf("%s\n", *it);
  19. return 0;
  20. }

——以上内容参考学习自 comzyh 的《算法竞赛初级杂烩包》,并进行整理

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注