CSND的c++语言的自己收藏夹内容汇总

浮点型数据求余:

使用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_permutationprev_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),取消控制小数点后的有效数字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值