<string> <string.h> <cstring> 的异同

本文深入解析了C++中的头文件string与string.h的区别,并详细阐述了iostream.h与iostream的区别,以及如何在C++代码中正确使用这些头文件。文章重点讲解了std命名空间的使用,包括如何导入并利用其中的cout进行输出,以及string类的创建与基本操作。此外,文章还对比了C与C++中字符串处理函数的不同,强调了string头文件的独特之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<string>是c++ 的头文件,其内包含了一个string类,string s1就是建立一个string类的对象 

<string.h> 的c语言的东西 并无类,所以不能 string s1 

<cstring>文件实际上只是在一个命名空间std中include了 <string.h>


~~~~~~~~~~~~~~~~······

头文件string与string.h的区别 

在C++中,#include<iostream>与#include<iostream.h>的区别,前者要使用更新的编译器(其实大部分编译器多比较前卫了,出了有些搞嵌入式的用变态的编译器)。

喔,原来iostream是C++的头文件,iostream.h是C的头文件,即标准的C++头文件没有.h扩展名,将以前的C的头文件转化为C++的头文件后,有时加上c的前缀表示来自于c,例如cmath就是由math.h变来的。

using namespace std //使用名字空间(使用所有)

using namespace std::cout//只使用cout

如不用using,则在代码前可以用sdt::cout<<表示使用的是std中的cout。

#include<iostream.h> //必须要加上.h
void main()
{
cout
<<"Right?"<<endl;
}

#include
<string>
#include
<iostream> //此处必须去掉.h
using namespace std ;
void main()
{
string s;
getline(cin,s);
cout
<<"Right?"<<endl;
}    

相关解析:

iostream.h里面定义的所有类以及对象都是在全局空间里,所以你可以直接用cout   
但在iostream里面,它所定义的东西都在名字空间std里面,所以你必须加上   
using namespace std才能使用cout

一般一个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名字空间的版本。

最大的挑战是把字符串头文件理清
楚:<string.h>是旧的C 头文件,对应的是基于char*的字符串处理函数;<string>
是包装了std 的C++头文件,对应的是新的string 类(看下文);<cstring>是对
应于旧C 头文件的std 版本。如果能掌握这些(我相信你能),其余的也就容易
了。


<string>是c++ 的头文件,其内包含了一个string类,string s1就是建立一个string类的对象 

<string.h> 的c语言的东西 并无类,所以不能 string s1 

<cstring>文件实际上只是在一个命名空间std中include了 <string.h>,…


#include<bits/stdc++.h> using namespace std; const int N = 100010; int pre[N]; int _rank[N];//树的深度 int cnt[N];//该连通块的点的个数; int find(int x){//找到根节点,并将前驱指向根节点 if (pre[x] == x) return x; else return pre[x] = find(pre[x]); } bool join(int x, int y) {//连接两个连通块 int tx = find(x); int ty = find(y); if (tx == ty) return 0; if (_rank[tx] < _rank[ty]) { pre[tx] = ty; cnt[ty] += cnt[tx]; } else { if (_rank[tx] == _rank[ty]) _rank[tx]++; pre[ty] = tx; cnt[tx] += cnt[ty]; } } bool isSame(int x, int y) { return find(x) == find(y); } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) {//初始化 pre[i] = i; _rank[i] = 1; cnt[i] = 1; } for (int i = 0; i < m; i++) { string c; int x, y; cin >> c; if (c == "C") { cin >> x >> y; join(x, y); } if (c == "Q1") { cin >> x >> y; if (isSame(x, y)) cout << "Yes\n"; else cout << "No\n"; } if (c == "Q2") { cin >> x; cout << cnt[find(x)] << endl; } } return 0; }#include <iostream> #include <algorithm> #include <cstring> #define int long long using namespace std; const int N = 1e5 + 10; int n , m; int p[N] , cnt[N]; int find(int x){ if (x != p[x]) p[x] = find(p[x]); return p[x]; } signed main() { cin >> n >> m; for (int i = 1 ; i <= n ; i ++ ) p[i] = i , cnt[i] = 1; while (m -- ) { string op; cin >> op; if (op == "C") { int x , y; cin >> x >> y; int px = find(x) , py = find(y); if (px != py) { p[px] = py; cnt[py] += cnt[px]; } } else if (op == "Q1") { int x , y; cin >> x >> y; int px = find(x) , py = find(y); if (px != py) cout << "No" << endl; else cout << "Yes" << endl; } else if (op == "Q2") { int temp; cin >> temp; cout << cnt[find(temp)] << endl; } } return 0; }
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值