STL,位运算,常用的库函数

STL:
1.
#include <vector>
vector是变长数组,支持随机访问,不支持在任何位置O(1)差入。为了保证效率,
元素的增删一般应该在末尾进行;
声名
    #include <vector>  头文件
    vector<int>a;      相当于一个长度动态变化的int 数组
    vector<int>b[233];  相当于第一维长233,第二维长度动态变化的int 数组
    struct rec{...};
    vector<rec>c;       自定义的结构体类型也可以保存在vector中
    size/empty
       size函数返回vector的实际长度(包含的元素个数),empty函数返回一个bool类
      型,表明vector是否为空。二者的时间复杂度都是O(1);
     clear
       clear函数把vector清空
     迭代器
     迭代器就像STL容器的“指针”,可以⽤星号“*”操作符解除引⽤。
      ⼀个保存int的vector的迭代器声明⽅法为:
       vector::iterator it;
        vector的迭代器是“随机访问迭代器”,可以把vector的迭代器与⼀个整数相加减,
        其⾏为和指 针的移动类似。可以把vector的两个迭代器相减,其结果也和指针相
        减类似,得到两个迭代器对应下标之间的距离。
       begin/end 
       begin函数返回指向vector中第⼀个元素的迭代器。例如a是⼀个⾮空的vector,
       则*a.begin() 与a[0]的作⽤相同。 
       所有的容器都可以视作⼀个“前闭后开”的结构[begin,end],end函数返回vector的尾部,
       即第n个元素再往后 的“边界”。
       *a.end()与a[n]都是越界访问,其中n=a.size()。
       front/back 
       front函数返回vector的第⼀个元素,等价于*a.begin() 和 a[0]。 
       back函数返回vector的最后⼀个元素,等价于*==a.end() 和 a[a.size() – 1]。
       push_back() 和 pop_back()
       这样行: 
       a.push_back(x) //把元素x插⼊到vector a的尾部。
       b.pop_back() //删除vector a的最后⼀个元素。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>a;
vector<int>b[233];
a.size();//求数组的长度
a.empty();//判断数组是否为空(属于bool变量)
a.clear();//清空当前数组
//迭代器的写法
vector<int>::iterator it=a.begin();//a的起始值的迭代器
a.end;//最后⼀个位置的下⼀位
vector a({1, 2, 3}); //初始化a数组 
cout << a[0] << ' ' << *a.begin() << endl; //输出值相同
遍历
1:
for(int i=0;i<a.size();i++)
cout<<a[i]<<endl;

2:
for(vector<int>::iterator i=a.begin();i!=a.end();i++)
cout<<*i<<' ';
cout<<endl;
3:
for(auto i=a.begin();i!=a.end();i++)
cout<<*i<<' ';
cout<<endl;
4:
for(int x:a)cout<<x<<' ';
cout<<endl;

cout<<a.front()<<' '<<a[0]<<' ' <<*a.begin()<<endl;//这三个一个意思
cout<<a.back()<<' '<<a[a.size()-1]<<endl;
}
2.
#include <queue>
头文件queue主要包括循环队列queue和优先队列priority_queue俩个容器。
循环队列先进先出,优先队列优先弹出所有数的最⼤值
声名
   queue<int>q;//队列
   struct res{...};
   queue<rec>q;//结构体res中必须定义小于号
   priority_queue<int>q; //大根堆,每次返回最⼤值
   priority_queue<int,vector<int>,greater<int>q;//小根堆,每次返回最⼩值
   priority_queue<pair<int.int>>q;
⼩根堆重载⼤于号,⼤根堆重载⼩于号
#include<iostream>
#include <queue>
using namespace std;
int main()
{
//构建结构体
struct Rec
{
int a,b;
bool operator<(const Rec& t)const//重载⼩于号
{
return a<t.a;
}
};
 priority_queue<int>d;
d.push({1,2});
}   
循环队列 queue<int>q; 
q.push(1); 在队头插⼊⼀个元素 
q.pop(); 弹出队尾元素 
q.front(); 返回队头元素 
q.back(); 返回队尾元素 
优先队列 priority_queue<int>a; 
a.push(1); 插⼊⼀个数 
a.pop(); 取最⼤值 
a.top(); 删除最⼤值 
注意:队列,优先队列,栈没有clear函数 
清空队列⽅法:重新初始化 q = queue();
3.
#include<stack>
头文件stack包含栈。声名和前面的容器类似。且弹出是先进后出,插⼊弹出都在队尾
{
stack<int> stk;
stk.top();返回栈顶
stk.push(1);向栈项插入
stk.pop();弹出栈项元素
}
4.
#include <deque>
双端队列deque是⼀个⽀持在两端⾼效插⼊或删除元素的连续线性存储空间。
 它就像是vector和queue的结合。与vector相⽐,deque在头部增删元素仅需要O(1)的时间; 
 与queue相⽐,deque像数组⼀样⽀持随机访问。
{
deque<int>a;
[] 随机访问
 a.begin/end(),返回deque的头/尾迭代器
  a.front/back() 队头/队尾元素 
  a.push_back() 在最后插⼊⼀个元素 
  a.push_front() 在开始插⼊⼀个元素 
  a.pop_back() 弹出最后⼀个元素 
  a.pop_front() 弹出第⼀个元素 
  a[0] 随机访问⼀个元素 
  a.clear() 清空队列
}
5.
#include<set>
    头⽂件set主要包括set和multiset两个容器,分别是“有序集合”和“有序多重集合”,
 即前者的元 素不能重复,⽽后者可以包含若⼲个相等的元素。
  set和multiset的内部实现是⼀棵红⿊树,它们⽀持的函数基本相同
声名
  set<int>s;
  struct rsc{...};
  set<rec>s;      // 结构体rec中必须定义⼩于号 
  multiset<double>s;
  set<int> a; // 元素不能重复   
  multiset<int> b; // 元素可以重复  
size/empty/clear  
与vector类似

迭代器  
set和multiset的迭代器称为“双向访问迭代器”,不⽀持“随机访问”,
⽀持星号(*)解除引⽤,仅 ⽀持”++”和--“两个与算术相关的操作。  
设it是⼀个迭代器
set<int>::iterator it=a.begin();
it++;it--;
++it;--it;
若把it++,则it会指向“下⼀个”元素。
这⾥的“下⼀个”元素是指在元素从⼩到⼤排序的结果中, 排在it下⼀名的元素。  
同理,若把it--,则it将会指向排在“上⼀个”的元素。
begin/end  
     返回集合的⾸、尾迭代器,时间复杂度均为O(1)。  
    s.begin() 是指向集合中最⼩元素的迭代器。  
   s.end() 是指向集合中最⼤元素的下⼀个位置的迭代器。
   换⾔之,就像vector⼀样,是⼀ 个“前闭后开”的形式。
    因此--s.end()是指向集合中最⼤元素的迭代器。
insert  
     s.insert(x)把⼀个元素x插⼊到集合s中,时间复杂度为O(logn)。  
    在set中,若元素已存在,则不会重复插⼊该元素,对集合的状态⽆影响。
find  
    s.find(x) 在集合s中查找等于x的元素,并返回指向该元素的迭代器。  
若不存在,则返回s.end()。时间复杂度为O(logn)。  可⽤来判断x在s中是否存在 
lower_bound/upper_bound  
        这两个函数的⽤法与find类似,但查找的条件略有不同,时间复杂度为 O(logn)。  
        s.lower_bound(x) 找到⼤于等于x的最⼩的元素的迭代器  
        s.upper_bound(x) 找到⼤于x的最⼩的元素的迭代器
erase  
      设it是⼀个迭代器,s.erase(it) 从s中删除迭代器it指向的元素,时间复杂度为O(logn)  
      设x是⼀个元素,s.erase(x) 从s中删除所有等于x的元素,时间复杂度为O(k+logn),
      其中k是被删除的元素个数。
count  
     s.count(x) 返回集合s中等于x的元素个数,时间复杂度为 O(k +logn),
     其中k为元素x的个数。  
     不存在x会返回0  
6.
#include <map>
     map容器是⼀个键值对key-value的映射,其内部实现是⼀棵以key为关键码的红⿊树。
Map的key 和value可以是任意类型,其中key必须定义⼩于号运算符。
 声明  
   map<key_type,valuue_type> name; 
   例如:  
   map<long, long, bool> vis;  
   mapint> hash;  
   mapint, int>, vector<int>> test; 
{
  map<string,vetcor<int>> a;
  a["yxc"]=vector<int>({1,2,3,4});
  cout<<a["yxc"][2]<<endl;
}
size/empty/clear/begin/end均与set类似。 
insert/erase  
    与set类似,但其参数均是pair<key_type,value_type>  
     可写为:a.insert({"a", {}}); 
find  
   h.find(x) 在变量名为h的map中查找key为x的⼆元组。 
[]操作符  
     h[key] 返回key映射的value的引⽤,时间复杂度为O(logn)。  
      []操作符是map最吸引⼈的地⽅。我们可以很⽅便地通过h[key]
来得到key对应的value,还可以对h[key]进⾏赋值操作,改变key对应的value。
7.
#include<unordered_set>
using namespace std;
int main()
{
unordered_set<int>s;//哈希表,不能存储重复元素的;
unordered_multiset<int>b;//哈希表可以存储重复元素,
}
8、 
#include<unordered_map>
9.
#include<bitset>
using namespace std;
int main()
{
bitset<1000>a;
a[0]=1;
a[1]=1;
a.count();
}
位运算
&  与  and
||  或 or
~  取反 not
^  异或 xor   //相同为0,不同为1.是从开头挨个⽐较,如6异或3等于5
<<  左移    相当于a乘以2的k次⽅
>>  右移   相当于a除以2的k次⽅ 若a = 110110 则右移⼀位等于11011 
常⽤操作:  
位数是从右开始,从0开始计数。  
求x的第k位数字 x >> k & 1  
lowbit(x) = x & -x,返回x的最后⼀位1
⼆进制中-a与~a+1相等 
库函数
#include<algorithm>
(1)reverse//翻转
翻转⼀个vector:
reverse(a.begin(), a.end());
翻转⼀个数组,元素存放在下标1~n: 
reverse(a + 1, a + 1 + n); 

(2)unique 去重
 返回去重之后的尾迭代器(或指针),仍然为前闭后开,即这个迭代器是去重之后末尾元素的下⼀ 
 个位置。
该函数常⽤于离散化,利⽤迭代器(或指针)的减法,可计算出去重后的元素个数。
把⼀个vector去重:
int m = unique(a.begin(), a.end()) – a.begin();
 
a.erase(unique(a.begin(), a.end()) – a.begin()); //将⼀个数组中的多余部分删
//去,⽐如原数组为1,1,2,3,3,4则输出结果为1,2,3,4  
把⼀个数组去重,元素存放在下标1~n:
int m = unique(a + 1, a + 1 + n) – (a + 1); 
(3)random_shuffle 随机打乱
   ⽤法与reverse相同
    写法:random_shuffle(a.begin(), a.end()); 

srand(time(0));//返回当前时间到1970年⼀⽉⼀⽇的秒数

输出随机值代码: 
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
using namespace std; 
int main() 
{  
vector<int> a({1, 2, 3, 4, 5});
    srand(time(0));      
    random_shuffle(a.begin(), a.end());    
    for (int x : a) cout << x] << ' ';  
    cout << endl;   
     return 0; 
      } 
(4)
sort
sort(a.begin(),a.end());//排序从小到大
sort(a.begin(),a.end(),greater<int>);//排序从大到小
自己定义
bool cmp(int a, int b) //a是否应该排在b的前⾯   
{  
return a > b; // 意为:如果a⼤于b,那么a排在b的前⾯   
}  
sort(a + 1, a + 1 + n, cmp); //⾃定义输出结果排序  
把⾃定义的结构体vector排序,重载“⼩于号”运算符: 
int main() 
{
   struct rec  
   {  
    int id, x, y;   
    }vector<rec> a;  
   bool operator <(const rec &a, const rec &b)   
   {  
   return a.x < b.x || a.x == b.x && a.y < b.y; 
    }  
   sort(a.begin(), a.end()); 
   (5)lower_bound/upper_bound ⼆分  
   lower_bound 的第三个参数传⼊⼀个元素x,在两个迭代器(指针)指定的部分上
   执⾏⼆分查 找,返回指向第⼀个⼤于等于x的元素的位置的迭代器(指针)。  
   upper_bound 的⽤法和lower_bound⼤致相同,唯⼀的区别是查找第⼀个⼤于x
   的元素。当然, 两个迭代器(指针)指定的部分应该是提前排好序的。
  在有序int数组(元素存放在下标1~n)中查找⼤于等于x的最⼩整数的下标:  
  int I = lower_bound(a + 1, a + 1 + n,. x) – a; 
  //括号⾥的参数分别为 a.begin(), a.end(), 要⽐较的值     
  在有序vector<int> 中查找⼩于等于x的最⼤整数(假设⼀定存在): 
   int y = *--upper_bound(a.begin(), a.end(), x);  
   写法代码及其意义:
#include<iostream> 
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int a[] = {1, 2, 4, 5, 6};
    vector<int> a{1, 2, 4, 5, 6};        //意义同上  
    
    int *p = lower_bound(a, a + 5, 7) - a;        //返回沿格第⼀个⼩于7的值      
    int *p = uppe_bound(a, a + 5, 7) - a;        //返回沿格第⼀个⼤于7的值     
    cout << *p << endl;
  
    return 0;
}
68. 0到n-1中缺失的数字   
 class Solution {
public:
    int getMissingNumber(vector<int>& nums) {
        unordered_set<int>s;//建立一个哈希表
        for(int i=0;i<=nums.size();i++)s.insert(i);//将0~n存入哈希表中
        for(auto x:nums)s.erase(x);//将nums遍历在哈希表中将其删除剩下没有出现过的
        return *s.begin();
    }
};  
32. 调整数组顺序使奇数位于偶数前面   
class Solution {
public:
    void reOrderArray(vector<int> &array) {
    int i=0,j=array.size()-1;//定义俩个指针
    while(i<j)
    {
        while(i<j&&array[i]%2)i++;//如果是奇数那么i就向右走
        while(i<j&&array[j]%2==0)j--;//如果是偶数那么j就向左走
        if(i<j)swap(array[i],array[j]);//如果俩个都停止在不是自己要求的,俩数交换
    }
    }
};   
20. 用两个栈实现队列   
class MyQueue {
public:
    /** Initialize your data structure here. */
    stack<int>s1,s2;//定义俩个栈
    MyQueue() {
        
    }
    
    /** Push element x to the back of queue. */
    void push(int x) {
        s1.push(x);//放入栈1
    }
    
    /** Removes the element from in front of queue and returns that element. */
    int pop() {
        while(s1.size()>1)s2.push(s1.top()),s1.pop();
        int t=s1.top();
        s1.pop();
        while(s2.size())s1.push(s2.top()),s2.pop();
        return t;
        //整体意思将s1中的弹出到s2中将s1中的第一个删除
    }
    
    /** Get the front element. */
    int peek() {
       while(s1.size()>1)s2.push(s1.top()),s1.pop();
        int t=s1.top();
        while(s2.size())s1.push(s2.top()),s2.pop();
        return t ;
        //和上面差不多就是将s2中的弹出到s1中
    }
    
    /** Returns whether the queue is empty. */
    bool empty() {
        return s1.empty();//判断s1这个栈是不是为空
    }
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * bool param_4 = obj.empty();
 */   
51. 数字排列 
next_permutation()//这个库函数可以将一段数按顺序依次不同的排列  
class Solution {
public:
    vector<vector<int>> permutation(vector<int>& nums) {
       sort(nums.begin(),nums.end());
       vector<vector<int>>res;
       do
       {
           res.push_back(nums);
       }
       while(next_permutation(nums.begin(),nums.end()));return res;
    }
    
};   
26. 二进制中1的个数   
class Solution {
public:
    int NumberOf1(int n) {
        int c=0;
        for(int i=0;i<32;i++)
        if(n>>i&1)
        c++;
        return c;
        
    }
};   
lowbit写法   
 class Solution {
public:
    //返回x的lowbit值
    int lowbit(int x)
    {
        return x & (-x);
    }
    int NumberOf1(int n) {
        int res = 0;
        while (n)
        {
            n -= lowbit(n);//每次减去lowbit(n)
            res ++;
        }
        return res;
    }
};  
 862. 三元组排序  
#include <iostream>
#include <algorithm>
using namespace std;
const int N=10010;
struct ccd
{
    int x;
    double y;
    string z;
    bool operator<(const ccd&t)const
    {
        return x<t.x;
    }
}a[N];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i].x>>a[i].y>>a[i].z;
    sort(a,a+n);
    for(int i=0;i<n;i++)
    printf("%d %.2lf %s\n",a[i].x,a[i].y,a[i].z.c_str());
return 0;
    
}   
   
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值