C/C++头文件含义

传统 C++
#include <assert.h> //设定插入点
#include <ctype.h> //字符处理
#include <errno.h> //定义错误码
#include <float.h> //浮点数处理
#include <fstream.h> //文件输入/输出
#include <iomanip.h> //参数化输入/输出
#include <iostream.h> //数据流输入/输出
#include <limits.h> //定义各种数据类型最值常量
#include <locale.h> //定义本地化函数
#include <math.h> //定义数学函数
#include <stdio.h> //定义输入/输出函数
#include <stdlib.h> //定义杂项函数及内存分配函数
#include <string.h> //字符串处理
#include <strstrea.h> //基于数组的输入/输出
#include <time.h> //定义关于时间的函数
#include <wchar.h> //宽字符处理及输入/输出
#include <wctype.h> //宽字符分类

====================================================================

标准 C++ (同上的不再注释)

#include <algorithm> //STL 通用算法
#include <bitset> //STL 位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL 双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL 定义运算函数(代替运算符)
#include <limits>
#include <list> //STL 线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include <iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL 队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL 堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL 通用模板类
#include <vector> //STL 动态数组容器
#include <cwchar>
#include <cwctype>

using namespace std;
 
ZT baidu Q&A
 
Q:vc2005中,同时有#include<stdio.h> #include<cstdio> ,然后调用printf,为什么不报错呢
#include<stdio.h> #include<cstdio>应该是向程序中引入了两个不同的库,这两个不同的库都有printf,编译器应该不知道调用哪个printf的,应该报错的,为什么没有报错呢。
 
A: 显然你的printf是<stdio.h>中的,要引用<cstdio>的这个函数应该这么用std::printf,还有你要记住,stdio.h是老式的C,C++头文件,cstdio是标准 C++(STL),且cstdio中的函数都是定义在一个名字空间std里面的,如果要调用这个名字空间的函数,必须得加std::或者在文件中声明use namespace std显然你的printf是<stdio.h>中的,要引用<cstdio>的这个函数应该这么用std::printf,还有你要记住,stdio.h是老式的C,C++头文件,cstdio是标准 C++(STL),且cstdio中的函数都是定义在一个名字空间std里面的,如果要调用这个名字空间的函数,必须得加std::或者在文件中声明use namespace std
 
Q:关于C语言中随机数的问题
 
C语言中随机数rand和srand有什么区别,为什么每次用rand的时候都要先用srand设置一随机数种子,用time.h作种子到底原理是什么的?randmos这个函数究竟该怎么用阿?设置随机数种子的要求是什么?格式是什么样的?到底怎么用才会比较好?time.h这是个什么函数阿??为什么有的函数用随机数的时候前会加上 int randoms(int number); 而有的却没有呢??很苦恼啊??使用随机数时候有什么基本的使用方法,请多讲讲啊!!不好意思,一次问的有点多,请帮帮忙吧~~
 
A:理论上,随机数不是随机产生的,他是有规律的,比如srand(1)到srand(100000)里面,有100000个种子,就是说你有100000个数可以选择,我们称之为随机,其实
主要关键是种子srand的选择是不是接近随机(不存在完全随机),书上说最好用的是时间,因为时间的数值随时间变化而变化,运行两次,一般不会出现前一次和后一次相同的局面,所以看上去就有点像随机了。
比如
这里是c++,c的我忘记了。
#include <time.h>
int a[10],b;
srand(time(0));
for (b=0;b<10;b++)
a[b]=rand()%100;
上面说的是用时间来作为种子,一天内不会重复,精确到秒,第二天,如果你的软件一直开着,就是说有1/(60*60*24)个可能会重复。如果长期使用,平均每使用60*60*24次,有一个概率会重复,当你发现重复现象,那时你把软件关掉,再开启,好像就可以重新安排种子里面的数字了。当然,条件是你有足够的数可供选择,所以a[b]=rand()%100;中的100可以写成60*60*24。
我上一个完整的c++程序,你下载dev-c++,运行一个就会发现问题。c的我忘的一干二净,真的写不出来。

#include <iostream.h>
#include <stdio.h>
#include <time.h>
main()
{int a[10],b,c,d;
srand(time(0));
for (b=0;b<10;b++)
{
a[b]=rand()%100;
}
for (b=0;b<10;b++)
{d=0;
for (c=2;c<a[b];c++)
{if (a[b]%c==0) d=1;
}
if (d==0) cout<<a[b]<<endl;
}
}
 
如果你要一个0-100以内的数字,那么要让电脑在1-100里随机选一个数

那怎么选呢,这个选数的东西就是种子,常用系统时钟作为种子,因此会有你说的time.h

比如系统时钟是由一个计数器来控制的,时钟不断在计时,数字不断在变化,假设计数范围是0-65535
当你取一个随机数时,就得到当前计数器的值,不同时候取的值不一样,差不多相当于是随机取的,但因为和时间有关,说不定会有什么不易察觉的规律,
所以这个随机数不是真正随机的数,是伪随机数(从其它文章里看来的)

然后用它除65535再乘100就相当于在0-100里随机取个数了
 
Q:c++ 浮点数格式化输出的问题
请问如何按照0.00的格式格式化输出一个浮点数:
我的想法是:
3.5 输出 03.50
10 输出 10.00 
A://使用标准C++编写
#include <iostream>
#include <iomanip>//精度设置必须包括的头文件

using namespace std;

int main()
{
double a=3.5;
int b=10;
//方法一:操作符函数的格式控制
//cout.precision(2),设置精度为2
//right:设置左对齐;fixed:控制浮点型输出格式;
//setw(5):设置输出位宽为5

cout<<right<<fixed<<setw(5)<<setfill('0')
<<setprecision(2)<<a<<endl; //输出结果为03.50

//方法二:IOS类成员函数的格式控制
cout.precision(4); //setprecision(4),设置精度为4
cout<<a<<endl; //输出结果为3.5000

//setfill('0'):设置填充字符为'0'
//static_cast<double>(b):将整型的b,
//生成一个双精度浮点型副本进行操作,而不改变其值和类型
cout<<fixed<<setfill('0')<<setprecision(2)
<<fixed<<static_cast<double>(b)<<endl;//输出10.00
return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <assert h>     设定插入点 #include <ctype h>     字符处理 #include <errno h>     定义错误码 #include <float h>     浮点数处理 #include <fstream h>    文件输入/输出 #include <iomanip h>    参数化输入/输出 #include <iostream h>    数据流输入/输出 #include <limits h>     定义各种数据类型最值常量 #include <locale h>     定义本地化函数 #include <math h>      定义数学函数 #include <stdio h>     定义输入/输出函数 #include <stdlib h>     定义杂项函数及内存分配函数 #include <string h>     字符串处理 #include <strstrea h>    基于数组的输入/输出 #include <time h>      定义关于时间的函数 #include <wchar h>     宽字符处理及输入/输出 #include <wctype h>     宽字符分类 int spawnvpe int mode char pathname char argv[] char envp[] spawn函数族在mode模式下运行子程序pathname 并将参数 arg0 arg1 arg2 argv[] envp[] 传递给子程序 出错返回 1 mode为运行模式 mode为 P WAIT 表示在子程序运行完后返回本程序 P NOWAIT 表示在子程序运行时同时运行本程序 不可用 P OVERLAY表示在本程序退出后运行子程序 在spawn函数族中 后缀l v p e添加到spawn后 所指定的函数将具有某种操作能力 有后缀 p时 函数利用DOS的PATH查找子程序文件 l时 函数传递的参数个数固定 v时 函数传递的参数个数不固定 ">#include <assert h>     设定插入点 #include <ctype h>     字符处理 #include <errno h>     定义错误码 #include <float h>     浮点数处理 #include <fstream h>    文件输入/输出 #include <iomanip h& [更多]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值