简体   繁体   English

C ++ std :: string和string

[英]C++ std::string and string

I don't suppose anyone tell me whats the difference between using the std::string and just the string data type in C++?? 我不认为有人告诉我在C ++中使用std :: string和字符串数据类型有什么区别?

In what situation should the std::string should be used over the standard string?? 在什么情况下应该在标准字符串上使用std :: string?

Thanks. 谢谢。

They're both the same type. 他们都是同一类型。 std::string specifies the namespace, while string only makes sense if a using namespace std; std::string指定名称空间,而string仅在using namespace std;时才有意义using namespace std; or using std::string statement is used. using std::string语句。 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 type const char [] , that implicitly decays in a const char * . 如果您在谈论字符串文字(例如"hello" ),那么它们的类型为const char [] ,它隐式地在const char *衰减。 std::string has a non- explicit constructor that takes a const char * , this allowing implicit conversion from const char * to std::string . std::string具有采用const char *的非explicit构造const char * ,这允许从const char *隐式转换为std::string The opposite operation can be done with the std::string::c_str() method. 可以使用std::string::c_str()方法完成相反的操作。

If you're looking for a reason to use std::string instead of const char * , then I guess my answer would be "as much as you can". 如果您正在寻找使用std::string而不是const char *的理由,那么我想我的回答将是“尽可能多”。

These are likely the same thing within your program. 这些在您的程序中可能是同一件事。 The "standard" string resides within the "std" namespace. “标准”字符串位于“ std”命名空间内。 If you are "using std::string;" 如果您正在“使用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. 如果未指定“ using”语句,则需要提供对象的名称空间。 It's preferable to not provide "using" statements within the header file and therefore you will typically see namespace resolution/specifiers on objects. 最好不要在头文件中提供“ using”语句,因此通常会在对象上看到名称空间解析/说明符。 (ie std::vector, mynamespace::myclass, etc.). (即std :: vector,mynamespace :: myclass等)。 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: 所以:

File: stdString.cpp
-------------
#include <string>
#include "providerx/string.h"

using namespace std;

void foo()
{
   string x;          // Using string object provided by the C++ standard library
   std::string y;     // Again, using C++ standard lib string
   providerx::string  // Using string object defined within the providerx namespace
}




File: providerString.cpp
-------------------
#include "providerx/string.h"

using providerX::string;

void foo()
{
    string x;   // Using providerx string object
}

Most likely, you have using namespace std somewhere in your code. 最有可能的是,您在代码中的某处using namespace std This is allowing you to access members of the std namespace without resolving the scope. 这允许您访问std名称空间的成员而无需解决范围。 Therefore... 因此...

std::string == string std :: string ==字符串

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

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