#include<iostream>与#include<iostream.h>及#inclue <string> 和 #include <string.h>的区别

本文详细解析了C++中不同I/O库的使用方法及其区别,包括<iostream>与<iostream.h>的不同之处,并探讨了<string>与<string.h>在字符串处理方面的应用差异。通过实例说明了如何正确使用这些库来实现字符串比较等功能。
 

#include<iostream>与#include<iostream.h>及#inclue <string> 和 #include <string.h>的区别

1.从功能性的角度来讲<iostream>包含了一系列模板化的I/O类,相反地<iostream.h>只仅仅是支持字符流。另外,输入输出流的C++标准规范接口在一些微妙的细节上都已改进,因此,<iostream>和<iostream.h>在接口和执行上都是不同的。最后,<iostream>的各组成都是以STL(Standard Template Library,标准模板库)的形式声明的,然而<iostream.h>的各组成都是声明成全局型的。 
     因为这些实质上的不同,你不能在一个程序中混淆使用这两个库。做为一种习惯,在新的代码中一般使用<iostream>,但如果你处理的是过去编写的代码,为了继承可以用继续用<iostream.h>就保持代码的一致性。

/////////////////////////////
<iostream>表示你使用的是标注命名空间,也就是在程序开始应该有这么一句话
using namespace std ; 这是遵循c++标准的
<iostream.h> 则没有遵循c++标准
/////////////////////////////
<string.h>是旧的C头文件,对应的是基于char*的字符串处理函数;
<string>是包装了std的C++头文件,对应的是新的strng类;
<cstring>是对应旧的C头文件的std版本。

(标准化委员会为了拉拢人心,吸引更多的人入会,决定为包装了std的那部分标准库构建新的头文件名。将现有C++头文件名中的.h去掉,所以就出现了< iostream.h> 和< iostream > 等很多双胞胎。对于C头文件,采用同样方法但在每个名字前还要添加一个C,所以C的<string.h> 变成了<cstring>。)

#include <iostream.h> 和 #include <iostream>  
前一个不是c++标准中的,后一个在c++标准中

还有就是平时我们所用的两种情况,当有输出和输入流时就要注意了。

换成#include <iostream>,要加一句using namespace std;或把cout改成std::cout,end改成std::endl等等

#include <iostream.h>
c++标准化之前的使用方式.
#include <iostream>
using namespace std;
这个必须同时出现.c++标准化后推荐使用的方式.主要是因为namespace的引入.

#include <..> 是从标准库里直接提取头文件
#include ".. "是先从源文件当前目录提取头文件,再从标准库里提取。

一般自己写的文件用 " "

2.为什么下面这段代码
#include <string.h>
void main()
{
     string aaa= "abcsd d";
     printf("looking for abc from abcdecd %s\n",
         (strcmp(aaa,"abc")) ? "Found" : "Not Found");
}
不能正确执行,说是string类型没有定义
而下面:
#include <string>
using namespace std;
void main()
{
     string aaa= "abcsd d";
     printf("looking for abc from abcdecd %s\n",
         (strcmp(aaa,"abc")) ? "Found" : "Not Found");
}
这里的string编译器就认识了,但是strcmp就不认识了呢?
---------------------------------------------------------------
一般一个C++的老的带“.h”扩展名的库文件,比如iostream.h,在新标准后的标准库中都有一个不带“.h”扩展名的相对应,区别除了后者的好多改进之外,还有一点就是后者的东东都塞进了“std”名字空间中。
但唯独string特别。
问题在于C++要兼容C的标准库,而C的标准库里碰巧也已经有一个名字叫做“string.h”的头文件,包含一些常用的C字符串处理函数,比如楼主提到的strcmp。
这个头文件跟C++的string类半点关系也没有,所以<string>并非<string.h>的“升级版本”,他们是毫无关系的两个头文件。
要达到楼主的目的,比如同时:
#include <string.h>
#include <string>
using namespace std;
或者
#include <cstring>
#include <string>
其中<cstring>是与C标准库的<string.h>相对应,但裹有std名字空间的版本。

微软企业开发技术 | 移动开发(Google Android、Windows Mobile)技术| 嵌入式系统设计与开发 | JAVA开发
a.cpp:1:3: error: extended character 、 is not valid in an identifier 1 | # 定义方向数组,按右、下、左、上的顺序 | ^ a.cpp:1:3: error: extended character 、 is not valid in an identifier a.cpp:1:3: error: extended character 、 is not valid in an identifier a.cpp:1:3: error: invalid preprocessing directive #\U00005b9a\U00004e49\U000065b9\U00005411\U00006570\U00007ec4\U0000ff0c\U00006309\U000053f3\U00003001\U00004e0b\U00003001\U00005de6\U00003001\U00004e0a\U00007684\U0000987a\U00005e8f 1 | # 定义方向数组,按右、下、左、上的顺序 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ a.cpp:6:3: error: invalid preprocessing directive #\U00005b58\U000050a8\U00006240\U00006709\U00008def\U00005f84\U0000548c\U00005bf9\U00005e94\U00007684\U00006b65\U00006570 6 | # 存储所有路径对应的步数 | ^~~~~~~~~~~~~~~~~~~~~~~~ a.cpp:8:19: warning: multi-character character constant [-Wmultichar] 8 | min_steps = float('inf') | ^~~~~ a.cpp:13:7: error: invalid preprocessing directive #\U00005224\U000065ad\U0000662f\U00005426\U00005230\U00008fbe\U00007ec8\U000070b9 13 | # 判断是否到达终点 | ^~~~~~~~~~~~~~~~ a.cpp:18:7: error: invalid preprocessing directive #\U00005c1d\U00008bd5\U000056db\U00004e2a\U000065b9\U00005411 18 | # 尝试四个方向 | ^~~~~~~~~~~~ a.cpp:22:11: error: invalid preprocessing directive #\U00005224\U000065ad\U000065b0\U00004f4d\U00007f6e\U0000662f\U00005426\U00005408\U00006cd5 22 | # 判断新位置是否合法 | ^~~~~~~~~~~~~~~~~~ a.cpp:36:7: error: invalid preprocessing directive #\U00008f93\U000051fa\U00006240\U00006709\U00008def\U00005f84\U0000548c\U00006b65\U00006570 36 | # 输出所有路径步数 | ^~~~~~~~~~~~~~~~~~ a.cpp:39:7: error: invalid preprocessing directive #\U00008f93\U000051fa\U00006700\U00005c11\U00006b65\U00006570 39 | # 输出最少步数 | ^~~~~~~~~~~~ a.cpp:40:27: warning: multi-character character constant [-Wmultichar] 40 | if min_steps == float('inf'): | ^~~~~ a.cpp:45:3: error: invalid preprocessing directive #\U0000793a\U00004f8b\U00008ff7\U00005bab 45 | # 示例迷宫 | ^~~~~~~~ a.cpp:2:1: error: ‘dx’ does not name a type 2 | dx = [0, 1, 0, -1] | ^~
最新发布
12-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值