C++ 何时使用 std::string 与 char*?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10937767/
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
When to use std::string vs char*?
提问by Noah Roth
Possible Duplicate:
C++ char* vs std::string
可能的重复:
C++ char* vs std::string
I'm new to C++ coming from C# but I really do like C++ much better.
我是来自 C# 的 C++ 新手,但我确实更喜欢 C++。
I have an abstract class that defines two constant strings (not static). And I wondered if a const char*
would be a better choice. I'm still getting the hang of the C++ standards, but I just figured that there really isn't any reason why I would need to use std::string in this particular case (no appending or editing the string, just writing to the console via printf
).
我有一个抽象类,它定义了两个常量字符串(非静态)。我想知道 aconst char*
是否是更好的选择。我仍然掌握了 C++ 标准的窍门,但我只是认为在这种特殊情况下我真的没有任何理由需要使用 std::string(没有附加或编辑字符串,只是写入控制台通过printf
)。
Should I stick to std::string
in every case?
我应该std::string
在每种情况下都坚持吗?
回答by Martin Beckett
Should I stick to std::string in every case?
我应该在每种情况下都坚持使用 std::string 吗?
Yes.
是的。
Except perhaps for a few edge cases where you writing a high performance multi-threaded logging lib and you really need to know when a memory allocation is going to take place, or perhaps in fiddling with individual bits in a packet header in some low level protocol/driver.
除了一些边缘情况,您编写高性能多线程日志库并且您确实需要知道何时进行内存分配,或者可能在某些低级协议中摆弄数据包标头中的各个位/司机。
The problem is that you start with a simple char and then you need to print it, so you use printf(), then perhaps a sprintf() to parse it because std::stream would be a pain for just one int to string. And you end up with an unsafe and unmaintainable mix oc c/c++
问题是你从一个简单的 char 开始,然后你需要打印它,所以你使用 printf(),然后可能使用 sprintf() 来解析它,因为 std::stream 只是一个 int 到字符串会很痛苦。你最终会得到一个不安全和不可维护的 oc c/c++ 组合
回答by Zach Dziura
I would stick to using std::string
instead of const char*
, simply because most of the built-in C++ libraries work with strings and not character arrays. std::string
has a lot of built-in methods and facilities that give the programmer a lot of power when manipulating strings.
我会坚持使用std::string
而不是const char*
,因为大多数内置的 C++ 库都使用字符串而不是字符数组。std::string
有很多内置的方法和设施,可以在操作字符串时赋予程序员很大的权力。
回答by K-ballo
Should I stick to std::string in every case?
我应该在每种情况下都坚持使用 std::string 吗?
There are cases where std::string
isn't needed and just a plain char const*
will do. However you do get other functionality besides manipulation, you also get to do comparison with other stringsand char arrays, and all the standard algorithms to operate on them.
有些情况下std::string
不需要,只需一个普通的char const*
就可以了。但是,除了操作之外,您确实获得了其他功能,还可以与其他字符串和字符数组进行比较,以及对它们进行操作的所有标准算法。
I would say go with std::string
by default (for members and variables), and then only change if you happen to see that is the cause of a performance drop (which it won't).
我会说std::string
默认使用(对于成员和变量),然后只有在您碰巧看到这是性能下降的原因(它不会)时才更改。
回答by Zaid Amir
This like comparing Apples to Oranges. std::string
is a container class while char* is just a pointer to a character sequence.
这就像将苹果与橙子进行比较。std::string
是一个容器类,而 char* 只是一个指向字符序列的指针。
It really all depends on what you want to do with the string.
这实际上完全取决于您想对字符串做什么。
Std::string on the other hand can give you a quick access for simple string calculation and manipulation function. Most of those are simple string manipulation functions, nothing fancy really.
另一方面,Std::string 可以让您快速访问简单的字符串计算和操作功能。其中大部分是简单的字符串操作函数,没什么特别的。
So it basically depends on your needs and how your functions are declared. The only advantage for std::string over a char pointer is that it doesnt require a specific lenghth decleration.
所以它基本上取决于你的需要以及你的函数是如何声明的。std::string 相对于 char 指针的唯一优势是它不需要特定的长度声明。
回答by Mark Ransom
Use std::string
when you need to store a value.
使用std::string
时,你需要存储的值。
Use const char *
when you want maximum flexibility, as almost everything can be easily converted to or from one.
const char *
当您需要最大的灵活性时使用,因为几乎所有内容都可以轻松转换为一个或从一个转换。