![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++
Blocking The Sky
这个作者很懒,什么都没留下…
展开
-
C++ 位运算计算二进制数中的1的个数
题目描述输入一个整数,输出该数二进制表示中1的个数。思路思路1将该整数与 1 进行与运算,判断末位是否为 1 ,然后将 1 左移一位,判断整数倒数第二位是否为 1 ,以此类推,不断左移 1 ,直到 1 左移 32 位。 sum =0; for(int j=0;j<31;j++){ if((z&i)==i){ sum++; } i=i<<1;原创 2021-05-28 22:37:12 · 1209 阅读 · 0 评论 -
c++ stack(栈)
基本用法:stack<int> s;stack< int, vector<int> > stk; //覆盖基础容器类型,使用vector实现stks.empty(); //判断stack是否为空,为空返回true,否则返回falses.size(); //返回stack中元素的个数s.pop(); //删除栈顶元素,但不返回其值s.top(); //返回栈顶元素的值,但不删除此元素s.push(item); //在栈顶压入新元素ite原创 2021-03-29 20:47:09 · 56 阅读 · 0 评论 -
c++ set(集合)
集合中的元素有三个特征:1.1 确定性(集合中的元素必须是确定的)。1.2 互异性(集合中的元素互不相同)。1.3 无序性(集合中的元素没有先后之分)。set容器是由红黑树实现的。在这里我们尽量把STL容器当作黑箱子使用,不需要知道原理,会用就行。由于set的一些特性,我们可以用来做一些特定的工作。十分方便。2.4 主要函数的使用方法2.4.1 声明一个set容器set<int> s;//下面用set<int> s作为例子 初始s:{1,2,3,4,5};.原创 2021-03-29 20:42:08 · 281 阅读 · 1 评论 -
c++ vector(不定长数组,向量)
下面就以邻接表为例展开介绍。struct edge{//定义边 int from,to,value;}const int maxn = 1e5+5;//声明:vector<edge> map[maxn];//一维向量相当于二维数组,便于理解。在这里,如果我们使用二维数组存邻接表,需要开销一个1e10的空间,如果图中实际边数只有1e6,用数组无疑会造成极大浪费。1.3.1 //初始化:for(int i=0;i<maxn;i++){ map[i].clear();原创 2021-03-29 20:38:15 · 512 阅读 · 0 评论 -
c++ random_shuffle(随机打乱序列)
对于一个数组a,我想打乱它原有的顺序,那么可以用该函数。注意,如果要真正的随机,需要srand(time(NULL))操作,因为random_shuffle用的是随机数发生器,还记得rand()吗,如果不srand随机数种子,得到的随机数是固定的。#include<bits/stdc++.h>using namespace std;int main(){ int a[1024]; for(int i=0;i<1024;i++)a[i]=i; srand(t原创 2021-03-29 20:15:37 · 4006 阅读 · 0 评论 -
c++ reverse(翻转序列)
#include<bits/stdc++.h>using namespace std;int main(){ //常用在字符串上 int a[5]={1,2,3,4,5}; reverse(a,a+5); //序列现在是 5 4 3 2 1 char s[]="exercise"; reverse(s,s+strlen(s)); //序列现在是 "esicrexe" //同样适用于string string b="原创 2021-03-29 20:13:10 · 1087 阅读 · 0 评论 -
c++ unique (去重)
如何把序列 a 中的重复元素去除呢?首先需要对原序列 a 进行排序,保证有序后,调用unique(a.head , a.tail )就可以了。unique会返回一个新长度,表示去重之后序列的长度。#include<bits/stdc++.h>using namespace std;int main(){ int a[]={1,3,5,7,9,2,2,2,1,1,1}; sort(a,a+11); int len = unique(a,a+11)-a; for(原创 2021-03-29 20:07:34 · 197 阅读 · 0 评论 -
c++ next_permutation (排列)
#include<bits/stdc++.h>using namespace std;int main(){ int a[10]={1,2,3}; do{ for(int i=0;i<3;i++) printf("%d ",a[i]); printf("\n"); }while(next_permutation(a,a+3)); return 0;}/*结果为:1 2 31 3 22原创 2021-03-29 20:03:39 · 48 阅读 · 0 评论 -
c++ lower_bound和upper_bound(二分查找)
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。在从小到大的排序数组中,lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到原创 2021-03-29 00:01:27 · 245 阅读 · 0 评论 -
c++ sort (排序)
sort(对序列进行排序,在algorithm头文件下),时间复杂度Olog(n)//数组排序:int a[10]={1,3,5,7,9,2,4,6,8,0};sort(a,a+10);//如果用的是vector,这样写vector<int> a;sort(a.begin(),a.end());//默认由小到大排序,当然我们可以重载排序的顺序bool cmp(int a,int b)//从大到小排序{ return a>b;}//然后sort(a,a+10,原创 2021-03-28 23:34:25 · 80 阅读 · 0 评论