C++常用函数

rand( )函数

  • 系统生成随机数:rand()*100生成0-99的随机数;
  • 所以rand()*100 + 1则是生成1-100的随机数
int num = rand()*100 + 1;	//生成1-100的随机数

srand( )函数

  • 用于添加随机数种子;
  • 作用利用当前系统时间生成随机数,防止每次随机数都一样
//time系统时间头文件包含
#include<ctime>

//利用当前系统时间生成随机数,防止每次随机数都一样
srand((unsigned int)time(NULL));
  • 在许多情况下,需要生成随机数。关于随机数生成器,有两个相关的函数。一个是 rand(),该函数只返回一个伪随机数。生成随机数之前必须先调用 srand() 函数。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
 
int main ()
{
   int i,j;
   // 设置种子
   srand( (unsigned)time( NULL ) );
 
   /* 生成 10 个随机数 */
   for( i = 0; i < 10; i++ )
   {
      // 生成实际的随机数
      j= rand();
      cout <<"随机数: " << j << endl;
   }
   return 0;
}

setw() setfill()输出 保留格式

 cout<<setw(5)<<setfill('0')<<a<<b;
 ##输出5位,右对齐,不足补0 
  • setw()与setfill()一般配对使用,用来保留格式;

1、setw(int n)只是对直接跟在<<后的输出数据起作用,而在之后的<<需要在之前再一次使用setw;
2、n是在输出时分配了n个字符的输出宽度,然后默认的是在n个字符宽度中左对齐输出
3、使用setfill(char x)使用指定字符变量x来填充空下的空格

setw()——汉译:set设置、w(width)宽度——即设置输出宽度;
setfill()——汉译:set设置、fill填充——即设置填充的字符;
所以两个一般配合使用;

#include<iostream>
using namespace std;
#include<string>
#include <iomanip>
int main() {
	int a = 123;
	int b = 11;
	cout << setw(5) << setfill('x') << a << ' ' << b<<endl;
	system("pause");	//暂停一下以便查看
	return 0;	//标准的返回退出
 }

注意:在使用时要加上下面的库

#include <iomanip>  ————库

使用场景:用于标准格式化输出:

以YY-MM-DD的形式打印,比如11-01-08
void Date::printStandardYear() {
	cout << this->year % 100 << this->separator;
	cout << setw(2) << setfill('0') << this->month;
	cout << this->separator << setw(2) << setfill('0') << this->day<<endl;
}

setprecision()/cout.precision()函数

C++中,想要保留数据的有效位数,需要用到setprecision()/cout.precision()函数。 具体用法如下:

  • 1、用setprecision()/cout.precision()函数都需要加上头文件:#include 。
    其中io代表输入输出,manip是manipulator(操纵器)的缩写,它主要是对cin,cout之类的一些操纵运算子,比如setfill,setw,setbase,setprecision等等。它是I/O流控制头文件,就像C里面的格式化输出一样。
  • 2、使用setprecision()/cout.precision()之后,如果不在修改精度,则之后所有的数据输出都是按照设置的精度来输出。
double a = 1.22023145, b = 0.123655, c = 2.2;
	cout.precision(3);
	//cout << setprecision(3);这里和用上面的那句程序效果是一样的。
	cout << a << endl;//这里是3位有效数字,输出1.22
	cout << b << endl;//输出0.123
	cout << c << endl;//输出2.2
  • 3、想要保留n为小数,需要加上cout.setf(ios::fixed); 或者cout << fixed或者cout<<setiosflags(ios::fixed) ;他们三个都是设置输入输出流用浮点数表示,再此过程中需要进行四舍五入。
double a = 1.22023145, b = 0.123655,c=2.2;
	
	//下面两行代码的作用一样,设置三位有效位数
	cout.precision(3);
	//cout << setprecision(3);
	
	//下面三行代码的作用一样。
	cout.setf(ios::fixed);
	//cout<<setiosflags(ios::fixed);
	//cout << fixed;
	
	cout << a << endl;//这里四舍五入,输出1.220
	cout << b << endl;//这里四舍五入,输出0.124
	cout << c << endl;//这里小数位数不够,需要补0,输出2.200

输出结果:保留三位小数,过程要进行四舍五入,位数不够的后面补0;

  • 4、在输出的正数前面加上+号,需要用到cout << showpos或者 cout.setf(ios::showpos)或者cout << setiosflags(ios::showpos);
double a = 1.22023145, b = -0.123655,c=2.2;
	
	//下面三行的代码作用一样,都是使输出的数据前加上+号,负数除外;
	cout << setiosflags(ios::showpos);
	//cout << showpos << endl;
	//cout.setf(ios::showpos);
	
	cout.precision(3);
	//cout << setprecision(3);
	
	cout.setf(ios::fixed);
	//cout<<setiosflags(ios::fixed);
	//cout << fixed;
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;

输出:

+1.220
-0.124
+2.200

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值