简体   繁体   English

const char与char数组vs std :: string的指针

[英]Pointer to const char vs char array vs std::string

Here I've two lines of code 在这里,我有两行代码

const char * s1 = "test";
char s2 [] = "test";

Both lines of code have the same behavior, so I cannot see any difference whether I should prefer s1 over s2 or vice-versa. 两行代码具有相同的行为,所以我看不出是否应该优先选择s1不是s2 ,反之亦然。 In addition to s1 and s2, there is also the way of using std::string . 除了s1和s2之外,还有使用std::string I think the way of using std::string is the most elegant. 我认为使用std :: string的方式是最优雅的。 While looking at other code, I often see that people either use const char * or char s [] . 在查看其他代码时,我经常看到人们use const char *char s [] Thus, my question is now, when should I use const char * s1 or char s [] or std::string ? 因此,我现在的问题是,何时应该使用const char * s1char s []std::string What are the differences and in which situations should I use which approach? 有什么区别,我应该在哪种情况下使用哪种方法?

POINTERS
--------

char const* s1 = "test";  // pointer to string literal - do not modify!

char* s1       = "test";  // pointer to string literal - do not modify!
                          //   (conversion to non-const deprecated in C++03 and
                          //       disallowed in C++11)

ARRAYS
------

char s1[5]     = "test";  // mutable character array copied from string literal
                          //    - do what you like with it!

char s1[]      = "test";  // as above, but with size deduced from initialisation



CLASS-TYPE OBJECTS
------------------

std::string s1 = "test";  // C++ string object with data copied from string
                          //    literal - almost always what you *really* want
const char * s1 = "test";
char s2 [] = "test";

These two aren't identical. 这两者并不完全相同。 s1 is immutable: it points to constant memory. s1是不可变的:它指向恒定的内存。 Modifying string literals is undefined behaviour. 修改字符串文字是未定义的行为。

And yes, in C++ you should prefer std::string . 是的,在C ++中你应该更喜欢std::string

The first one is constant, the second isn't. 第一个是常数,第二个不是。 std::string is a class type and implements many useful functions and methods for string manipulation, making it much easier and user-friendly. std :: string是一个类类型,它为字符串操作实现了许多有用的函数和方法,使其更容易和用户友好。 The c-style 'strings' with char pointers are difficult to control, manipulate and often cause errors, but don't have the overhead the std::string has. 带有char指针的c风格“字符串”很难控制,操作并经常导致错误,但是没有std :: string的开销。 Generally it's better to stick to the std::strings cause they're easier to maintain. 通常最好坚持使用std :: strings,因为它们更容易维护。

The only difference between the two that you should care about is this: 你应该关心的两者之间的唯一区别是:

Which one is your project already using? 您的项目已经使用了哪一个?

These two do not have the same behavior. 这两个没有相同的行为。 s1 is a simple pointer which is initialized to point to some (usually read-only) area of the memory. s1是一个简单的指针,它被初始化为指向存储器的某个(通常是只读的)区域。 s2 , on the other hand, defines a local array of size 5, and fills it with a copy of this string. 另一方面, s2定义了一个大小为5的本地数组,并用该字符串的副本填充它。

Formally, you are not allowed to modify s1 , that is, do something like s1[0] = 'a' . 在形式上,您不能修改s1 ,即执行类似s1[0] = 'a' In particular, under weird circumstances, it could cause all other "test" s in your program to become "aest" , because they all share the same memory. 特别是,在奇怪的情况下,它可能会导致程序中的所有其他"test"成为"aest" ,因为它们都共享相同的内存。 This is the reason modern compilers yell when you write 这就是现代编译器在你写作时大喊大叫的原因

char* s = "test";

On the other hand, modifying s2 is allowed, since it is a local copy. 另一方面,允许修改s2 ,因为它是本地副本。

In other words, in the following example, 换句话说,在以下示例中,

const char* s1 = "test";
const char* s2 = "test";
char s3[] = "test";
char s4[] = "test";

s1 and s2 may very well point to the same address in memory, while s3 and s4 are two different copies of the same string, and reside in different areas of memory. s1s2可以很好地指向存储器中的相同地址,而s3s4是相同字符串的两个不同副本,并且驻留在存储器的不同区域中。

If you're writing C++, use std::string unless you absolutely need an array of characters. 如果您正在编写C ++,请使用std::string除非您绝对需要一个字符数组。 If you need a modifiable array of characters, use char s[] . 如果需要可修改的字符数组,请使用char s[] If you only need an immutable string, use const char* . 如果您只需要一个不可变的字符串,请使用const char*

which one to be used depends upon your requirement. 哪一个使用取决于您的要求。 Pointer offers you more flexiblity. 指针为您提供更多灵活性。 and in some cases vulerability. 在某些情况下,有毒。 Strings are a safe option and they provide Iterator support. 字符串是一种安全的选择,它们提供Iterator支持。

除非你知道为什么需要一个char数组/指向char的指针,否则使用std::string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM