cout 语句中的字符串文字和字符串之间的区别?

发布于 2025-01-11 23:40:51 字数 147 浏览 2 评论 0 原文

以下打印字符串的方式有什么区别?

std::cout << "hello";

// and

std::cout << std::string("hello");

他们似乎都在做同样的事情。

What is the difference between the following ways of printing a string?

std::cout << "hello";

// and

std::cout << std::string("hello");

They both appear to do the same thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2025-01-18 23:40:52

情况 1

这里我们考虑以下语句:

std::cout << "hello";

在上面的语句中,"hello" 是一个 const char[6] 类型的字符串文字,其中由于类型衰减,>衰减const char*。然后将此 const char* 传递给 operator<< 给出如下:

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
                                         const char* s );

情况 2

这里我们考虑语句:

std::cout << std::string("hello");

在上面的语句中,表达式std::string("hello") 创建一个临时 std::string 对象。现在,由于 std::string 已重载 operator<<(如下所示),将调用 operator<< 的重载版本并执行其主体。

template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
               const std::basic_string<CharT, Traits, Allocator>& str);

Case 1

Here we consider the statement:

std::cout << "hello";

In the above statement "hello" is a string literal of type const char[6] which decays to a const char* due to type decay. This const char* is then passed to the non-member overload of operator<< given below:

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
                                         const char* s );

Case 2

Here we consider the statement:

std::cout << std::string("hello");

In the above statement the expression std::string("hello") creates a temporary std::string object. Now since the std::string has overloaded operator<<(given below), that overloaded version of operator<< will be called and its body will be executed.

template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
               const std::basic_string<CharT, Traits, Allocator>& str);
笑饮青盏花 2025-01-18 23:40:52

std::cout << "hello"; 使用运算符<< /code> 处理以 null 结尾的字符数组的重载。 "hello",它是一个 const char[6],传递时衰减const char*作为运算符<<重载的参数:

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
                                         const char* s );

std::cout << std::string("hello"); 使用 operator<< 重载。 std::stringstd::basic_string、std::allocator>< 的 typedef /代码>。

template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
               const std::basic_string<CharT, Traits, Allocator>& str);

std::cout << "hello"; uses the operator<< overload dealing with null terminated character arrays. "hello", which is a const char[6], decay s into a const char* when passed as an argument to the operator<< overload:

template< class CharT, class Traits >
basic_ostream<CharT,Traits>& operator<<( basic_ostream<CharT,Traits>& os,
                                         const char* s );

std::cout << std::string("hello"); uses the operator<< overload for dealing with strings. std::string is a typedef for std::basic_string<char, std::char_traits<char>, std::allocator<char>>.

template <class CharT, class Traits, class Allocator>
std::basic_ostream<CharT, Traits>&
    operator<<(std::basic_ostream<CharT, Traits>& os,
               const std::basic_string<CharT, Traits, Allocator>& str);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文