义乌公司做网站,南沙网站建设公司,怎么样做公司官网,pc网站怎么建设流程一.sort排序
快排的改进算法#xff0c;评价复杂度为(nlogn).
1.用法
sort(起始地址#xff0c;结束地址下一位#xff0c;*比较函数) [起始地址#xff0c;结束地址) (左开右闭)
#includebits/stdc.h
using namespace std;
int main()
{//sortvectorint评价复杂度为(nlogn).
1.用法
sort(起始地址结束地址下一位*比较函数) [起始地址结束地址) (左开右闭)
#includebits/stdc.h
using namespace std;
int main()
{//sortvectorint v;int x;while (cin x) v.push_back(x);sort(v.begin(), v.end());for (int num : v) cout num ;//范围forcout endl;} 2.自定义函数
当sort没有默认参数时默认为升序如果想要改为降序就需要自己写个函数然后利用回调函数实现。
如果想要小于,参数为(int a,int bcmp)返回值 return ab 代表第一个数小于第二个数 即非升序
#includebits/stdc.h
using namespace std;
bool cmp(int a, int b)
{return a b;
}
int main()
{//sortvectorint v;int x;while (cin x) v.push_back(x);//使用while(cinx) 按ctrlz停止加回车停止输入sort(v.begin(), v.end(),cmp);for (int num : v) cout num ;//范围forcout endl;} 3.练习
对数组升序然后再降序。
#includebits/stdc.h
using namespace std;
int main()
{//sortvectorint v;int x;while (cin x) v.push_back(x);//使用while(cinx) 按ctrlz停止加回车停止输入sort(v.begin(), v.end());for (int num : v) cout num ;//范围forcout endl;for (int i v.size()-1; i 0; i--) cout v[i] ;cout endl;}
二.memset()
void*(void* p,int value,size_t num)用于初始化内存块的值p是要填充的内存块value为要设置的值8位二进制数num为字节数。
1.适用范围
memset是一个字节字节进行初始化的所以如果你是一个整数比如1的话那他的内存存储就会是00000001000000010000000100000001结果就会是一个很大的数而不是每一个数都为1但如是初始化一个字符数组那字符数组每个字符都是1个字节那就刚刚好整数只能是-1或0
字符数组
#includebits/stdc.h
using namespace std;
int main()
{char a[4];memset(a, 1, sizeof(a));//cout a;//因为一开始没有对char初始化没有斜杠零 所以后面会输出烫烫for (int i 0; i 4; i) cout a[i];}
整型数组出现的情况(只能初始化为0和-1)
#includebits/stdc.h
using namespace std;
int main()
{int a[4];memset(a, 1, sizeof(a));//cout a;//因为一开始没有对char初始化没有斜杠零 所以后面会输出烫烫for (int i 0; i 4; i) cout a[i] ;} 三.swap()
swap(T a,T b);
交换两个变量的值 int a10 int b20 swapab 四.reserve()
用于反转容器元素reverse(first,last)[first,last),左闭右开
using namespace std;
int main()
{vectorint v;int x;while (cin x) v.push_back(x);for (int num : v) cout num ;cout endl;reverse(v.begin(), v.end());for (int num : v) cout num ;} 五.unique() 去重函数唯一函数
unique(first,last)[first,last)函数接受两参数
first指向要去重的第一个元素的迭代器
endlast最后一个元素的下一个
返回值返回去重后最后一个元素的下一个迭代器比如122333排序完为123233返回值就是123后的三的地址之后使用erase就可以后面的那些多余元素。
#includebits/stdc.h
using namespace std;
int main()
{vectorint v {1,2,2,3,3,3,4,4,4,4};auto itunique(v.begin(), v.end());for (int num : v) cout num ;cout endl;v.erase(it, v.end());for (int num : v) cout num ;} 六.next_permutation()和prev_permutation函数 next_permutation()和prev_permutation函数分别对应生成当前序列的下一个排列、上一个排列。next_permutation他按照字典序即从大到小排序对序列进行重新排列如果存在下一个排列则将当前序列改为下一个排序并返回true如果当前为最后一个排列则改为第一个排列并且返回falseprev_permutation及对应上一个排列存在返回true反之改为第一个序列并返回false。
练习
输出1234的所有全排列
#includebits/stdc.h
using namespace std;
int main()
{vectorint v {1,2,3,4};bool flag true;while (flag){for (int num : v) cout num;cout endl;flag next_permutation(v.begin(), v.end());}//最后一个序列继续的话变为第一个序列cout endl;//多空一行好分清for (int num : v) cout num;cout endl;} 七.binary_search和lower_bound和upper_bound
1.binary_search
binary_search是c的标准库函数通过二分查找算法博客里有单独的一个讲解来确定是否存在目标元素返回值为bool
三个参数第一个左闭右开[left,right),最后一个参数为目标值
#includebits/stdc.h
using namespace std;
int main()
{ //binary_searchvectorint v {1,2,3,4};int target 4;bool found binary_search(v.begin(), v.end(), target);if (found) cout 找到了 endl;else cout 没找到;
} 2.lower_boundn和upper_bound
lower_bound(first,end,x),区间仍为左闭右开返回值为返回第一个大于等于x的元素地址不是下标。
upper_bound(first,end,x),区间也是左闭右开返回值为第一个大于x的元素地址。
由于返回值是一个地址想要得到下标就必须减去首地址v.begin()。
解释一下他们名字的含义lower_bound名为下界upper_bound为上界当x为7时候如下图 例子
找第一个大于等于8的位置
#includebits/stdc.h
using namespace std;
int main()
{ //binary_searchvectorint v {8,9,5,44,3};//二分查找的前提是单调先进行排序sort(v.begin(), v.end());cout (lower_bound(v.begin(), v.end(), 8)-v.begin());
}