C 字符串和 C++ 字符串有什么区别?

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

whats the difference between C strings and C++ strings?

c++cstring

提问by Teja

whats the difference between C Strings and C++ strings. Specially while doing dynamic memory allocation

C 字符串和 C++ 字符串有什么区别。特别是在进行动态内存分配时

回答by paxdiablo

I hardly know where to begin :-)

我几乎不知道从哪里开始:-)

In C, strings are just chararrays which, by convention, end with a NUL byte. In terms of dynamic memory management, you can simply mallocthe space for them (including the extra byte). Memory management when modifying strings is yourresponsibility:

在 C 中,字符串只是char按照惯例以 NUL 字节结尾的数组。在动态内存管理方面,您可以简单地malloc为它们分配空间(包括额外的字节)。修改字符串时的内存管理是您的责任:

char *s = strdup ("Hello");
char *s2 = malloc (strlen (s) + 6);
strcpy (s2, s);
strcat (s2, ", Pax");
free (s);
s = s2;

In C++, strings (std::string) are objects with all the associated automated memory management and control which makes them a lot safer and easier to use, especially for the novice. For dynamic allocation, use something like:

在 C++ 中,字符串 ( std::string) 是具有所有相关联的自动内存管理和控制的对象,这使得它们更安全、更易于使用,尤其是对于新手而言。对于动态分配,请使用以下内容:

std::string s = "Hello";
s += ", Pax";

I know which I'dprefer to use, the latter. You can (if you need one) always construct a C string out of a std::stringby using the c_str()method.

我知道更喜欢使用哪个,后者。您可以(如果需要)始终std::string使用该c_str()方法从 a 中构造一个 C 字符串。

回答by Chandan Singh

C++ strings are much safer,easier,and they support different string manipulation functions like append,find,copy,concatenation etc.

C++ 字符串更安全、更简单,它们支持不同的字符串操作函数,如追加、查找、复制、连接等。

one interesting difference between c string and c++ string is illustrated through following example

通过以下示例说明了 c 字符串和 c++ 字符串之间的一个有趣区别

#include <iostream>                            
using namespace std;

int main() {
    char a[6]; //c string
    a[5]='y';
    a[3]='o';
    a[2]='b';
    cout<<a; 
    return 0;
}

output ??boRy¤£f·Pi??

输出??boRy¤£f·Pi??

#include <iostream> 
using namespace std; 
int main() 
{ 
  string a; //c++ string
  a.resize(6);
  a[5]='y';
  a[3]='o';
  a[2]='b';
  cout<<a;
  return 0;
}

output boy

输出男孩

I hope you got the point!!

我希望你明白了!!

回答by Rav Singh Sandhu

The difference is speed in some operations, and encapsulation of places where common errors occur. std::string maintains information about the content and length of the string. This means it is not prone to buffer overruns in the same way a const char * is. It will also be faster for most operations than a const char * because the length of the string is known. Most of the cstring operations call strlen() under the hood to find out the length of the string before operating on it, this will kill performance. std::string is compatible with STL algorithms and other containers.

不同之处在于某些操作的速度,以及常见错误发生的地方的封装。std::string 维护有关字符串内容和长度的信息。这意味着它不像 const char * 那样容易发生缓冲区溢出。对于大多数操作来说,它也比 const char * 更快,因为字符串的长度是已知的。大多数 cstring 操作都会在底层调用 strlen() 来找出字符串的长度,然后再对其进行操作,这会降低性能。std::string 与 STL 算法和其他容器兼容。

Lastly, std::string doesn't have the same "gotcha!" moments a char * will have:

最后, std::string 没有相同的“陷阱!” char * 将具有的时刻: