哪个 C++ 头文件声明了 strlen?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19107845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:31:12  来源:igfitidea点击:

which C++ header file declares strlen?

c++strlen

提问by user2756494

Which library does strlen()belong to?

strlen()属于哪个图书馆?

Does it belong to cstring? or string?

它属于cstring? 或者string

I tried the following code, and it does work:

我尝试了以下代码,它确实有效:

include <iostream>
using namespace std;
//withou include<string.h>

int main() {
    char * str="abc";
    cout<<strlen(str);
}

I set strequal to 3 and give the right answer 3.

我设置str等于 3 并给出正确答案 3。

Why does it work without including library string or cstring?

为什么不包含库字符串或 cstring 就可以工作?

Should I include cstring or string there? string.h?

我应该在那里包含 cstring 或 string 吗?字符串.h?

回答by Adam Rosenfield

Which library does strlen() belong to? Does it belong to cstring? or string?

strlen() 属于哪个库?它属于cstring吗?或字符串?

Neither. cstringand stringare not libraries, they are header fileswhich define the interfaceto various functions and classes.

两者都不。 cstring并且string不是,它们是定义各种函数和类的接口的头文件

The C language standard says that the strlenfunction is declared in the header file <string.h>. In C++, including <string.h>places strleninto the global namespace, while including <cstring>instead places strleninto the stdnamespace.

C 语言标准说strlen函数是在头文件中声明的<string.h>。在 C++ 中,包括<string.h>放置strlen到全局命名空间中,而包括<cstring>代替放置strlenstd命名空间中。

The actual implementationof the strlenfunction is in the C standard library (aka libcor CRTon certain platforms). Ordinarily, this is linked in with your executable at link time.

实际执行中的strlen功能是在C标准库(又名libcCRT在某些平台上)。通常,这在链接时与您的可执行文件链接。

Why it works without including library string or cstring?

为什么它不包含库字符串或 cstring 就可以工作?

In your particular compiler and toolchain, it just so happens that the header file <iostream>includes <cstring>into it, which means that any code that includes the former also gets the latter for free. This is an implementation detailand should not be relied upon—if your compile your code with another compiler, you may suddenly find yourself in a sea of compiler errors.

在您的特定编译器和工具链中,头文件恰好<iostream>包含<cstring>在其中,这意味着包含前者的任何代码也会免费获得后者。这是一个实现细节,不应该被依赖——如果你用另一个编译器编译你的代码,你可能会突然发现自己陷入了编译器错误的海洋。

The proper thing to do is to also include <cstring>here; even though it's not necessary with your particular compiler, it may be necessary with other compilers.

正确的做法是也包括<cstring>在这里;即使您的特定编译器不需要它,但其他编译器可能需要它。

回答by MBI

In order to use strlen() you need to include cstring header file.

为了使用 strlen() 你需要包含 cstring 头文件。

#include <cstring>

This was the answer I was looking for, but didn't find direct answer here.

这是我一直在寻找的答案,但在这里没有找到直接答案。

回答by slash28cu

The function strlen() is declared in the header file string.h

函数 strlen() 在头文件 string.h 中声明

From the GNU C Library

来自 GNU C 库

回答by Ammar

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

int main()
{
    char *pointer1="Idle hands are the devil's workshop.";

    int length;
    length=strlen(pointer1);

    char *pointer2;
    pointer2=new char(length+1);

    strcpy(pointer2,pointer1);

    cout<<"pointer 2:"<<pointer2;
    delete[] pointer2;

    return 0;
}

回答by Brian Conley

#include <stdio.h>
#include <string.h>

int main(void)
{
    char *string = "Hello World";
    printf("%lu\n", (unsigned long)strlen(string));
    return 0;
}

This program will print the value 11, which is the length of the string "Hello World". Character strings are stored in an array of a data type called char. The end of a string is found by searching for the first null character in the array.

该程序将打印值 11,即字符串“Hello World”的长度。字符串存储在称为 char 的数据类型的数组中。通过搜索数组中的第一个空字符来找到字符串的结尾。

From the GNU C Library

来自 GNU C 库