浮点型数据求余:
使用c++库函数:fmod,fmodf,fmodl
fmod:用于double型
fmodf:用于float型
fmodl:用于long doudle型
double f=3.3,q=2.2
cout<<fmod(f,q)//打印1.1
全排列函数:
STL提供的计算排列关系的算法:
next_permutation和prev_permutation
int a[4]={1,2,4,3};
next_permutation(a,a+4);
for(int i=0;i<4;i++)
{
cout<<a[i]<<" ";// 1 2 4 3
}
//————————分割线————————
int a[4]={1,2,4,3}
prev_permutation(a,a+4);
for(int i=0;i<4;i++)
{
cout<<a[i]<<" ";// 1 2 3 4
}
c++的报错
字符串的截取
int main(){
string s="hello world";
string a=s.substr(2);//llo world
string b=s.substr(1,4);//ello
//第一个参数为截取的起始坐标,第二个参数为截取长度
cout<<a<<" "<<b;
}
字符串与数字之间的相互转换
字符串转数字:
使用stoi(),stof(),stod(),stol(),stold()
int main(){
string s="1234567890";
int a=stoi(s);
cout<<a;//数字1234567890
}
字符数组转数字:
使用atoi(),atol(),atof()
int main(){
char s[10]="012345678";
long long a=atol(s);
cout<<a;//12345678
}
字符串转数字:
使用to_string()
int main(){
long long s=123456789;
string a=to_string(s);
a="0"+a;
cout<<a;//01234567890
}
字符数组转数字:
使用itoa()
int main(){
int a=100;
char s[10];
itoa(100,s,2);//第一参数个为整数,第二个参数为字符数组,第三个为进制
cout<<s;//1100100
}
C++浮点型控制精度
1.使用cout输出时默认输出6位有效数字
2.使用cout.precision()控制输出有效数字
3.使用定点法,cout.flags(cout.fixed),将控制输出小数点后面的有效数字
4.取消定点法,cout.unsetf(cout.fixed),取消控制小数点后的有效数字