<1>next_permutation()
next_permutation()
函数的作用就是将给定序列转换为其字典序中的下一个排列。如果当前排列已经是字典序中的最后一个排列,该函数会将序列重置为字典序中的第一个排列(即元素按升序排列的排列),并返回 false
;否则,它会将序列转换为下一个排列,并返回 true
。
int a[10];
int main(){
a[1] = 3,a[2] = 1,a[3] = 2,a[4] = 4;
bool tag = true;
while(tag){
for(int i =1;i<=4;++i) cout<<a[i]<<' ';
cout<<'\n';
tag = next_permutation(a+1,a+1+4);
}
return 0;
}
<2>memset(ptr,value,num)函数 ( 对于非字符类型的数组可能会产生未定义行为 )
ptr指向要设置值的内存块的指针,value指要设置的值,通常是一个整数,num指要设置的字节数
int a[5];
memset(a,0,sizeof a); //第二位数字为0或-1可以得到想要的值 其他值不直观
for(int i = 0;i < 5;++i) cout << a[i] << '\n';
<3>swap()函数
可以交换两个数的值
int c=9;int b=8;
cout<<b<<c;
cout<<'\n';
swap(b,c);
cout<<b<<c;
<4>reverse()函数
可以把一个序列(如数组、向量、字符串等)中指定区间的元素顺序颠倒过来。
vector<int> vec = {1,2,3,4,5};
reverse(vec.begin(),vec.end());
for(auto i:vec)
cout<<i<<' ';
<5>unique()函数
可以去除容器中相邻重复元素的函数
vector<int> v = {1,1,2,2,3,3,4,4};
auto it = unique(v.begin(),v.end());
v.erase(it,v.end());//erase 函数的作用是移除 vector 中那些经过 unique 函数处理后产生的多余元素,从而保证 vector 中只保留唯一的元素
for(int num : v){
cout<<num<<'\n';
}