C++ std::string 和 string
我想没有人告诉我使用 std::string 和 C++ 中的字符串数据类型有什么区别?
在什么情况下应该使用 std::string 而不是标准字符串?
谢谢。
I don't suppose anyone tell me whats the difference between using the std::string and just the string data type in C++??
In what situation should the std::string should be used over the standard string??
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们俩是同一类型。
std::string
指定命名空间,而string
仅在using namespace std;
或using std::string< 时才有意义使用 /code> 语句。所以你使用什么并没有什么区别(只要你保持一致)。
如果您谈论的是字符串文字(例如
"hello"
),那么它们的类型为const char []
,在中隐式衰减常量字符*
。std::string
有一个非显式
构造函数,它接受const char *
,这允许从const char *< 进行隐式转换/code> 到
std::string
。可以使用std::string::c_str()
方法完成相反的操作。如果您正在寻找使用
std::string
而不是const char *
的理由,那么我想我的答案是“尽可能多”。They're both the same type.
std::string
specifies the namespace, whilestring
only makes sense if ausing namespace std;
orusing std::string
statement is used. So it doesn't make any difference what you use (as long as you're consistent).If you're talking about the string literals (such as
"hello"
), then they're of typeconst char []
, that implicitly decays in aconst char *
.std::string
has a non-explicit
constructor that takes aconst char *
, this allowing implicit conversion fromconst char *
tostd::string
. The opposite operation can be done with thestd::string::c_str()
method.If you're looking for a reason to use
std::string
instead ofconst char *
, then I guess my answer would be "as much as you can".这些可能与您的程序中的相同。 “standard”字符串位于“std”命名空间内。如果您正在“使用 std::string;”或“使用命名空间 std;”在模块内,那么它们是等价的。如果您没有指定“using”语句,则需要提供对象的名称空间。最好不要在头文件中提供“using”语句,因此您通常会在对象上看到名称空间解析/说明符。 (即 std::vector、mynamespace::myclass 等)。 “using”的使用在实现文件中更常见,它们不会像在头文件中指定那样影响其他文件。
可以使用来自其他提供者的字符串对象。在这种情况下,提供者将/应该在自己的命名空间中实现字符串对象,并且您需要声明您正在使用哪个字符串对象。
所以:
These are likely the same thing within your program. The "standard" string resides within the "std" namespace. If you are "using std::string;" or "using namespace std;" within the module, then they are equivalent. If you aren't specifying a "using" statement, then you are required to provide the namespace of the object. It's preferable to not provide "using" statements within the header file and therefore you will typically see namespace resolution/specifiers on objects. (i.e. std::vector, mynamespace::myclass, etc.). The use of "using" is more common in implementation files where they won't affect other files as they would if specified in a header file.
It's possible to use a string object from another provider. In this case the provider would/should have their string object implementation in their own namespace and you would need to declare which string object you were using.
So:
最有可能的是,您的代码中某处有
using namespace std
。这允许您访问 std 命名空间的成员,而无需解析范围。因此...std::string == string
Most likely, you have
using namespace std
somewhere in your code. This is allowing you to access members of the std namespace without resolving the scope. Therefore...std::string == string