为什么你喜欢char *而不是字符串,在C ++? [英] Why do you prefer char* instead of string, in C++?

查看:133
本文介绍了为什么你喜欢char *而不是字符串,在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个C程序员试图编写c ++代码。我听说C ++中的 string 在安全性,性能等方面优于 char * char * 是一个更好的选择。有人建议程序员不要在C ++中使用 char * ,因为我们可以做所有 char *



你在C ++中使用过 char * 吗?具体条件是什么?

解决方案

使用 std :: string 因为你不需要担心为字符串分配/取消分配内存。 C ++ std :: string 类可能在内部使用 char * 数组。但是,类将为您管理内部数组的分配,重新分配和释放。这消除了使用原始指针所带来的所有常见风险,例如内存泄漏,缓冲区溢出等。



此外,它也非常方便。您可以复制字符串,附加到字符串等,而无需手动提供缓冲区空间或使用像strcpy / strcat这样的函数。使用std :: string就像使用 = + 操作符一样简单。



基本上,它是:

  std :: string s1 =Hello 
std :: string s2 = s1 +World;

对比...

  const char * s1 =Hello; 
char s2 [1024]; //我真的应该在这里分配多少?
strcpy(s2,s1);
strcat(s2,World);



编辑



响应您对C ++中使用 char * 的编辑:许多C ++程序员会声明您应该使用 char * ,除非你正在使用一些需要它的API / legacy函数,在这种情况下你可以使用 std :: string :: c_str code>函数来将 std :: string 转换为 const char *



但是,我会说在C ++中有一些C数组的合法用法。例如,如果性能是绝对关键的,堆栈上的小C阵列可能是比 std :: string 更好的解决方案。你也可能正在编写一个程序,你需要绝对控制内存分配/释放,在这种情况下你会使用 char * 。另外,正如在注释部分所指出的, std :: string 不能保证为你提供一个连续的,可写的缓冲区*,所以你不能直接写从文件到 std :: string ,如果你需要你的程序是完全可移植的。但是,在需要这样做的情况下, std :: vector 仍然可能比使用原始C数组更好。






*尽管在C ++ 11中,这已经改变,使得 std :: string 确实提供了一个连续的缓冲区


I'm a C programmer trying to write c++ code. I heard string in C++ was better than char* in terms of security, performance, etc, however sometimes it seems that char* is a better choice. Someone suggested that programmers should not use char* in C++ because we could do all things that char* could do with string, and it's more secure and faster.

Did you ever used char* in C++? What are the specific conditions?

解决方案

It's safer to use std::string because you don't need to worry about allocating / deallocating memory for the string. The C++ std::string class is likely to use a char* array internally. However, the class will manage the allocation, reallocation, and deallocation of the internal array for you. This removes all the usual risks that come with using raw pointers, such as memory leaks, buffer overflows, etc.

Additionally, it's also incredibly convenient. You can copy strings, append to a string, etc., without having to manually provide buffer space or use functions like strcpy/strcat. With std::string it's as simple as using the = or + operators.

Basically, it's:

 std::string s1 = "Hello ";
 std::string s2 = s1 + "World";

versus...

 const char* s1 = "Hello";
 char s2[1024]; // How much should I really even allocate here?
 strcpy(s2, s1);
 strcat(s2, " World ");

Edit:

In response to your edit regarding the use of char* in C++: Many C++ programmers will claim you should never use char* unless you're working with some API/legacy function that requires it, in which case you can use the std::string::c_str() function to convert an std::string to const char*.

However, I would say there are some legitimate uses of C-arrays in C++. For example, if performance is absolutely critical, a small C-array on the stack may be a better solution than std::string. You may also be writing a program where you need absolute control over memory allocation/deallocation, in which case you would use char*. Also, as was pointed out in the comments section, std::string isn't guaranteed to provide you with a contiguous, writable buffer *, so you can't directly write from a file into an std::string if you need your program to be completely portable. However, in the event you need to do this, std::vector would still probably be preferable to using a raw C-array.


* Although in C++11 this has changed so that std::string does provide you with a contiguous buffer

这篇关于为什么你喜欢char *而不是字符串,在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆