在 C++ 中获取 const char * 长度的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44690087/
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-28 15:37:37  来源:igfitidea点击:

Best way to get length of const char * in c++

c++string-lengthstrlen

提问by JamesAlb

i know two way's to get length of const char *

我知道两种获取 const char * 长度的方法

const char * str = "Hello World !";
int Size = 0;
while (str[Size] != '
const char * str = "Hello World !";
size_t Size = strlen(str);
') Size++;

and other way is very simple

其他方式很简单

#include <cstddef>
#include <cstring>

int string_size_1()
{
    const char * str = "Hello World !";
    int Size = 0;
    while (str[Size] != '
string_size_1():                     # @string_size_1()
        mov     eax, 13
        ret

string_size_2():                     # @string_size_2()
        mov     eax, 13
        ret
') Size++; return Size; } int string_size_2() { const char * str = "Hello World !"; size_t Size = strlen(str); return Size; }

but i don't want to use str lib functions like strlenand i think this function use my first way behavior too. because in the pc world when we want to count something we need to count each block's and there is no magic to get the length with one movement so i think the first way is the best option to get length of const char *. other way i think the first way is maybe too heavy for heavy string's. so im confused. which way is better and why other way is not ?

但我不想使用 str lib 函数,strlen而且我认为这个函数也使用我的第一种方式行为。因为在 pc 世界中,当我们想要计算某些东西时,我们需要计算每个块的数量,并且没有魔法可以通过一次移动来获得长度,所以我认为第一种方法是获得长度的最佳选择const char *。另一种方式我认为第一种方式对于重弦来说可能太重了。所以我很困惑。哪种方式更好,为什么其他方式不是?

回答by

Let's inspect the assembly listing of these two methods.

让我们检查一下这两种方法的汇编列表。

template <std::size_t S>
constexpr std::size_t string_length
(
    char const (&)[S]
)
{
    return S - 1;
}

Using Clang 4.0.0 with flags -std=c++14 -O2

使用带有标志的 Clang 4.0.0 -std=c++14 -O2

std::cout << string_length("example") << std::endl;

Link: https://godbolt.org/g/5S6VSZ

链接:https: //godbolt.org/g/5S6VSZ

Both methods end up with exactly the same assembly listing. Also, the compiler optimizes away everything and just return a constant because the string literal is known during compile-time. So, in terms of performance, they are equally good.

这两种方法最终都得到完全相同的程序集列表。此外,编译器优化了所有内容并只返回一个常量,因为在编译时字符串文字是已知的。因此,就性能而言,它们同样出色。

But in terms of readability, strlen(str)is definitely better. A function call states the intention through the function name. A loop cannot do that.

但在可读性方面,strlen(str)肯定更好。函数调用通过函数名称说明意图。循环无法做到这一点。



Besides, std::stringand std::string_vieware more preferable than C-string in many cases. Consider them.

此外,在许多情况下,std::stringstd::string_view比 C-string 更可取。考虑他们。

回答by Michael Maniscalco

In this case the answer is known at compile time:

在这种情况下,答案在编译时已知:

##代码##

usage:

用法:

##代码##

For cases where the string is not a compile time constant use strlen if only the pointer to the string is available, std::distance if both pointers to the beginning and end are available, or .size() if your dealing with a std::string

对于字符串不是编译时常量的情况,如果只有指向字符串的指针可用,则使用 strlen,如果指向开头和结尾的指针都可用,则使用 std::distance ,如果处理 std,则使用 .size() : :细绳