• C++ 输入输出


    C++ i/o

    C++的发展(c plus plus)

    1) 对C的语法做了扩展

    2) 面向对象编程(思想)

    3)C++版本

    98 03 11 14 17 20 23

    相关网站: isocpp.org

    ​ zh.cppreference.com

    工具:vs2019

    c++对C的提升

    类型敏感:

    	int n = 8;
    	int n1 = 5.6;
    	int n2 = 3.4f;
    
    	//int n3 = { 5.6 };    
    	//int n3 = { 5 };
    	//char ch = { 0x8755 };
    

    使用{}赋值,类型严格匹配

    类型占位符:

    	//类型占位符,类型推到符,根据=右侧的值推导出左侧变量的类型
    	int n = 9;
    	auto n0 = 3;//3是int类型,所以推导出no是int类型
    	auto p = "test";
    	auto f = 3.6f;
    	//p=55;//报错,p是const char*类型,不能赋值int类型的值
    
    	//auto z;//报错,没有值可以用于推到Z的类型
    	decltype(p) p1 = "world";//使用p的类型推导p1的类型
    	decltype(p) p2;
    	//p2=5;//报错,根据p的类型推导到p2的类型为 const char*,所以不能赋值int类型的值
    	//类型比较复杂的时候
    	char************* ary[12];
    	decltype(ary[0]) nData = ary[1];	
    

    空指针:

    	char* p = NULL;//宏
    	int n = NULL;//可读性差,第一眼看过去,以为n是指针类型
    	char* p1 = nullptr;//关键字
    	//int n = nullptr;//报错,nullptr只能用于指针类型。
    

    范围迭代

    	//范围迭代
    	int ary[] = { 12,5,6,7,8,21,174,2,4,872,7 };
    	for (int i = 0; i < sizeof(ary) / sizeof(ary[0]); i++)
    	{
    		printf("%d", ary[i]);
    	}
    	printf("
    ");
    	for (int val : ary)
    		printf("%d", val);
    	printf("
    ");
    

    输出

    std::cout 定义头文件

    万物皆可 hello world

    	std::cout << "hello world" << std::endl;//作用域运算符,end1相当于
    
    	std::cout << 5 << std::endl;
    	std::cout << 5.3f << std::endl;
    	std::cout << 6.3 << std::endl;
    

    可以全局定义 using namespace std;//命名空间

    可以省去cout前面的std::

    	cout << "hello world" << std::endl;//作用域运算符,end1相当于
    
    	cout << 5 << std::endl;
    	cout << 5.3f << std::endl;
    	cout << 6.3 << std::endl;
    

    可以再省略一点

    cout << "hello world" << " "
    		<< 5 << " "
    		<< 5.3f << " "
    		<< 6.3 << " "
    		<< std::endl;
    

    进制

    cout默认输出十进制

    调用setf()

    	cout.setf(ios_base::hex, ios_base::basefield);
    	cout << 0x213 << std::endl;
    	cout.setf(ios_base::oct, ios_base::basefield);
    	cout << 1234 << std::endl;
    	cout.setf(ios_base::dec, ios_base::basefield);
    	cout << 133 << std::endl;
    

    计数方式

    	cout << std::fixed << 13.5687 << endl;
    	cout << std::scientific << 13.54896 << endl;
    	cout << std::scientific << -13.54896 << endl;
    
    	cout.precision(3);
    	cout << std::fixed << 13.5687 << endl;
    	cout << std::scientific << 13.5687 << endl;
    	cout.unsetf(ios_base::scientific);//取消科学计数法,恢复默认的定点法
    	cout.precision(4);
    	cout << 536.78545665 << std::endl;
    
    

    对齐

    显示前缀 showbase

    image-20201217012824436

    显示(不显示)浮点数的点showpoint & noshowpoint


    剩下功能如上图。

    注意:std::uppercase用于字符中没效果,要用于十六进制的字符大小写。

    输入

    cin的一般使用

    	int n;
    	float f;
    	double dbl;
    	char szBuff[255] = { 0 };
    	cin >> n >> f >> dbl >> szBuff;
    

    在控制台中直接输入数据。

    cin>>szBuff>>n;//输入hello world 22,会出错,空格截断导致后面的输入出错
    

    应该新建缓冲区,读取大小直接丢掉。

    	int n;
    	
    	char szBuff[255] = { 0 };
    	
    	cin >> szBuff;
    
    	int nCntAvail = cin.rdbuf()->in_avail();
    	cin.ignore(nCntAvail);//从缓冲区读取大小,再ignore
    	cin >> n;
    

    如果想要把空格也一起读取。可以使用 cin.getline

    char arr[255]={0};
    cin.getline(ary,sizeof(ary))//读一行
    
  • 相关阅读:
    centOS7 mini配置linux服务器(三) 配置防火墙以及IPtables切换
    Linux的常用基本命令。
    centOS7 mini配置linux服务器(二) 配置IP
    分享Git的一些个人配置
    Firefox中Vimperator插件配置
    Linux下修改键盘默认布局
    Git对于单个文件的分批提交方式的使用
    Linux安装Monaco字体
    浮点与整形在GUI下的相关思考
    2D简单图形相关算法罗列
  • 原文地址:https://www.cnblogs.com/pupububu/p/14160676.html
Copyright © 2020-2023  润新知