一
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int cmp(int a,int b)//从大到小
{
return a>b;
}
void print(int *temp,int n)//打印数组
{
for(int i=0;i<=n-1;i++)
{
cout<<temp[i]<<" ";
}
cout<<endl;
}
int main()
{
int a[10]={9,9,7,7,6,5,4,4,3,2};
int b[11]={9,9,7,7,6,5,4,4,3,2,100};
int *s=max_element(a,a+10);//最大元素
cout<<*s<<endl;//9
s=min_element(a,a+10);//最小元素
cout<<*s<<endl;//2
int N=unique(a,a+10)-a;//去重,返回去重后数组的尾地址
cout<<N<<endl;//2
print(a,N);
sort(b,b+10);
print(b,10);
sort(b,b+10,cmp);
print(b,10);
///以下两个为初始化函数
memset(b,0,sizeof(b)); //按位初始化,只能初始化为0 1 -1
print(b,11);
int x;
cin>>x;
fill(b,b+10,x);//按一定区间[b,b+10)初始化为x
print(b,11);
return 0;
}
二
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
char str[5]="abcd";
int len=strlen(str);
cout<<str<<endl;
while(next_permutation(str,str+len))
{
//从这个区间向后查找还有字典序吗?没有的话返回false
cout<<str<<endl;
}
char str1[5]="dcba";
len=strlen(str1);
cout<<str1<<endl;
while(prev_permutation(str1,str1+len))
{
//从这个区间向前查找还有字典序吗?没有的话返回false
cout<<str1<<endl;
}
return 0;
}
三
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int data;
data=atoi("123");//a char 字符串转换成整型 同理 atol atoll atof strtoi stroll strtof
cout<<data<<endl;
char buf[512];
sprintf(buf,"%o",data);//转换八进制,等价于buf=itoa(data);
cout<<buf<<endl;
//int float double long long string
int i=1750;
char buffer[33];
itoa(i,buffer,10);//i转换成十进制保存到字符串中
printf("decimal:%s\n",buffer);
itoa(i,buffer,16);//i转换成十六进制保存到字符串中
printf("hexadecimal:%s\n",buffer);
itoa(i,buffer,2);//i转换成二进制保存到字符串中
printf("binary:%s\n",buffer);
return 0;
}