首页 > 技术文章 > C++常用STL

love-study-chase 2020-07-18 11:58 原文

C++基本操作

vector

image-20200324164637124image-20200324172640320

image-20200324172937806
如何查找 第二维?或者第一维

【链接】对vector中的pair进行多次find操作

class isE{
  isE(int val) :User(val){}
  bool operator()(const pair<int,int>& e)const{
    return e.second == User;
  }
  const int User;
}
vector<pair<int,int>> res;
auto it = find_if(res.begin(), res.end(),isE(val));返回一个迭代器

排序
bool cmp(PII x, PII y){
  return x.first > y.first;
}//从大到小排
sort(res.begin(), res.end(),cmp);

迭代器

image-20200324173546158

string

image-20200324173757083
  • 插入操作全是O(n)复杂度的
image-20200324173941567

algorithm

''#include

快速排序

image-20200324174749889 image-20200324175859554 image-20200324180008641

函数重载

image-20200324180116485* 符号重载

image-20200326093309640

nth_element 排序 找第n号元素(从第零号开始)

image-20200326093722346 image-20200326094008608

stack 栈

image-20200326094524051

queue队列

pair类型

https://blog.csdn.net/Enterprise_/article/details/73695255

image-20200326094604579

stack 和 queue 时间复杂度 加入 和删除操作 O(1)

优先队列 按照优先级插入 O(log n)

set集合

image-20200326094918537

集合是排好序的

map

image-20200326095148731 image-20200326095410805 image-20200326095645535 image-20200326095758337

![](/Users/yangfan/Library/Application Support/typora-user-images/image-20200326095744892.png)

Multiset multimap 改为unordered_set, unordered_map

image-20200326100126997

不需要理解每一个细节~~

www.cplusplus.com/reference/ 认真查看STL用法

自己瞎调

image-20200326100402000

10e6数据能用O(nlog n) 再大 只能用O(n)

https://vjudge.net

排序nth_element

语法 nth_element(arr, arr + k, arr + arr.size()) 就是将arr中第 k+1小的数放在 arr[k]的位置上

//
 nth_element(arr, arr + k, arr + arr.size()) ;//第k+1小

  bool cmp(int a, int b){
      return a > b;
  }
  nth_element(arr, arr + k, arr + arr.size(), cmp); //第k+1大


二分查找API

lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。

在从小到大的排序数组中,

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

在从大到小的排序数组中,重载lower_bound()和upper_bound()

lower_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,greater<type>() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

#include<bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
const int INF=2*int(1e9)+10;
#define LL long long
int cmd(int a,int b){
	return a>b;
}
int main(){
	int num[6]={1,2,4,7,15,34}; 
	sort(num,num+6);                           //按从小到大排序 
	int pos1=lower_bound(num,num+6,7)-num;    //返回数组中第一个大于或等于被查数的值 
	int pos2=upper_bound(num,num+6,7)-num;    //返回数组中第一个大于被查数的值
	cout<<pos1<<" "<<num[pos1]<<endl;
	cout<<pos2<<" "<<num[pos2]<<endl;
	sort(num,num+6,cmd);                      //按从大到小排序
	int pos3=lower_bound(num,num+6,7,greater<int>())-num;  //返回数组中第一个小于或等于被查数的值 
	int pos4=upper_bound(num,num+6,7,greater<int>())-num;  //返回数组中第一个小于被查数的值 
	cout<<pos3<<" "<<num[pos3]<<endl;
	cout<<pos4<<" "<<num[pos4]<<endl;
	return 0;	
} 

推荐阅读