C++11没有#include\<string\>也可以使用string

C++11没有#include<string>也可以使用string

在c++11中,这段代码可以运行

//并未包含<string>
#include <iostream>
using namespace std;

int main()
{
   string s1 = "Hello";
   cout<<s1<<endl;
   return 0;
}

能运行的原因如下:

  1. std::istream 和 std::ostream 都有 std::ios_base 这个基类。

  2. std::ios_base 的成员函数 getloc 以值返回 std::locale (要求定义可见)。

  3. std::locale 的成员函数 name 以值返回 std::string (要求定义可见)。

所以#include<iostream> 后能使用std::string,不过不一定等价于#include<string>

个人建议:使用string变量时,包含string头文件

参考文章:https://blog.csdn.net/qq_34201858/article/details/110486124

posted on   coderabcd  阅读(505)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示