strlen()函数在#include 的区别

Sodino 2014-10-22 09:59:47
想使用strlen()函数。但发现有这么两种情况
如果使用#include <cstring> 或#include <string.h>则是正常运行的
但当使用#include <string>则错误了,错误提示如下:



21:55:52 **** Build of configuration Debug for project VariableLab ****
make all
'Building file: ../ExternFile.cpp'
'Invoking: GCC C++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"ExternFile.d" -MT"ExternFile.d" -o "ExternFile.o" "../ExternFile.cpp"
../ExternFile.cpp: In function 'int main()':
../ExternFile.cpp:12:41: error: 'strlen' was not declared in this scope
cout << str << " strlen=" << strlen(str) << endl;
^
make: *** [ExternFile.o] Error 1

21:55:53 Build Finished (took 735ms)


代码:

#include <iostream>
#include <string> // #include <cstring>或者#include <string.h>则是正常运行的
using namespace std;
int main() {
char str[] = "China";
cout << str << " strlen=" << strlen(str) << endl;
return 0;
}




于是有了以下这几个问题?
1. 这是为什么呢?
2. 为什么有这三个头文件?各有什么区别呢?
3. 如何确定一个函数(现在是strlen(),要是以后是其它的啥 function_xx())是在哪个头文件中呢?

不胜感激!!
...全文
567 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 7 楼 mujiok2003 的回复:
[quote=引用 5 楼 brookmill 的回复:] <string.h>是c语言的头文件,包括strlen、strcpy之类的库函数声明。 <cstring>实际上就是string.h,只不过在c++里面改个名字而已。 <string>是c++特有的,c语言里没有,它包括了stl的string类,但是不包括c语言的库函数声明。
that's answer to the second question. [/quote] 有帮助
赵4老师 2014-10-23
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
mujiok2003 2014-10-23
  • 打赏
  • 举报
回复
引用
3. 如何确定一个函数(现在是strlen(),要是以后是其它的啥 function_xx())是在哪个头文件中呢?
http://www.cplusplus.com/strlen 把strlen换成你别的函数名字吧。
mujiok2003 2014-10-23
  • 打赏
  • 举报
回复
引用 5 楼 brookmill 的回复:
<string.h>是c语言的头文件,包括strlen、strcpy之类的库函数声明。 <cstring>实际上就是string.h,只不过在c++里面改个名字而已。 <string>是c++特有的,c语言里没有,它包括了stl的string类,但是不包括c语言的库函数声明。
that's answer to the second question.
brookmill 2014-10-23
  • 打赏
  • 举报
回复
引用 4 楼 sodino 的回复:
3. 如何确定一个函数(现在是strlen(),要是以后是其它的啥 function_xx())是在哪个头文件中呢?
这个要查手册或者帮助信息。微软系列的就看msdn,linux的就用man
brookmill 2014-10-23
  • 打赏
  • 举报
回复
<string.h>是c语言的头文件,包括strlen、strcpy之类的库函数声明。 <cstring>实际上就是string.h,只不过在c++里面改个名字而已。 <string>是c++特有的,c语言里没有,它包括了stl的string类,但是不包括c语言的库函数声明。
Sodino 2014-10-22
  • 打赏
  • 举报
回复
主要是想知道第2 和第3个问题 2. 为什么有这三个头文件?各有什么区别呢? 3. 如何确定一个函数(现在是strlen(),要是以后是其它的啥 function_xx())是在哪个头文件中呢?
Sodino 2014-10-22
  • 打赏
  • 举报
回复
谢谢u012421456
我使用的eclipse...

toolchain是MinGW GCC
百曉生 2014-10-22
  • 打赏
  • 举报
回复

百曉生 2014-10-22
  • 打赏
  • 举报
回复
楼主什么编译器(IDE) 我的dev c++和c-free都没问题 应该是各种工具自带的库不太一样吧(大致是这个意思) 具体不太了解,楼主自己可以搜一下 右击到定义

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧