热血英豪七尺魔剑:为什么我的C++无法使用string.h?

来源:百度文库 编辑:高校问答 时间:2024/05/10 19:58:15
在C里面,对于字符串,只能使用字符串数组进行处理。
在C++里面,据说有了string型的数据类型,需要在程序开头输入#include <string.h>然后就能在程序中用 string 变量名 来进行数据的定义。
但是我按照说明做了,编译无法通过,说是类型名错误,也就是说无法使用string对变量进行定义。
请高手解释一下原因和解决办法。谢谢!
比如说:
#include <iostream.h>
#include <string.h>
main()
{
char c; //定义一个字符变量C,这个可以实现
string cc;//定义一个字符串变量CC,这个就不能实现了
}

#include <iostream>
#include <string>
using namespace std;

int main()
{
char c; //定义一个字符变量C
string cc;//定义一个字符串变量CC
//...
return 0;
}

<iostream>中有string型的定义。但要用到名字空间std:using namespace std;
若要用C语言中的字符串处理函数,如:strcat、strlen、strncmp等,只要再加一个#include <cstring>就行了.

输入#include <string.h>只是对char型的可以直接用c
++自带的函数可以直接使用而已,象复制,比较char大小之类的 ,变量还是要自己定义的

头文件这样写
#include <iostream>
#include <string>
using namespace std;