C++学习笔记(一)
来源:互联网 发布:网络安全法答题 编辑:程序博客网 时间:2024/06/03 21:57
本文主要内容为C++下的输入输出函数
以及for循环中的C++11新特性
。
一、输入输出函数
1. cin
cin
遇到 空格
、回车
、Tab
结束输入, 且会将读到的空格、回车、Tab 丢弃,例:
#include<iostream>using namespace std;int main(void) { char a[10]; cin >> a; cout << a << endl; //第一次输出 cin >> a; cout << a << endl; //第二次输出 return 0;}
Test:
由图像可见,在输入时,若输入两串字符,即便程序需要输入两次,第二次读取不会再次手动从键盘输入,而是上次输入的空格后的内容成为第二次读入的字符串。类似于scanf
。
2. cin.get()
1) cin.get()有三种形式:
ch = cin.get();
cin.get( ch );
cin.get( str, size );
注:前两个用于读取字符,第三个用于读取字符串。cin.get()不会丢弃读到的空格、回车,即第一个cin.get()读到 Space 或 Enter 后,停止读取数据,在第二次的cin.get()中,会将上次读取到的 Space 或 Enter 存到字符中,而不是继续读取结束符后面的数据。
#include<iostream>using namespace std;int main(void) { char a, b, c; cout << "input: "; cin.get(a); cout << "output : " << a << endl; cout << "input again: "; cin.get(b); cout << "output : " << b << endl; cout << "input again: "; cin.get(c); cout << "output : " << c << endl; return 0;}
Test:
2) 在使用cin.get(str, size)
读取字符串时,同样会遇到Space与Enter结束输入,而且不会将最后读到的Space与Enter丢弃,所以会造成后续cin.get()无法继续读入应有的字符串。PS:可用cin.clear()
来清除
#include<iostream>using namespace std;int main(void) { char a[10], b[10], c[10]; cout << "input: " << endl; cin.get(a, 10); cout << "output : " << a << endl; cout << "input again: " << endl; cin.get(b, 10); cout << "output : " << b << endl; cout << "input again: " << endl; cin.get(c, 10); cout << "output : " << c << endl; return 0;}
Test:
3. cin.getline()
cin.getline(str, size);
用于读取一行字符串,限制读入size - 1
个字符,会自动将读到的Enter
丢弃,且会在字符串最后添加\0
作为结束符。然而,如果一行字符串长度超过 size ,会造成多余字符丢失。
#include<iostream>using namespace std;int main(void) { char a[3], b[3], c[3]; cin.getline(a, 3); cin.getline(b, 3); cin.getline(c, 3); cout << "output : " << a << endl; cout << "output : " << b << endl; cout << "output : " << c << endl; return 0;}
Test :
图中两次运行程序均只输入了一次,第一次输入`abcdefg`,测试长度超限,第二次测试空格对其影响。
二、循环(C++11 新特性)
C++11新增了一种循环:基于范围(range-based)的for循环。
(吐槽一下~~,虽然在学C++,但是总是会想到Python,因为好多东西貌似Python……,就比如这个range-based,Python的循环为for i in range(1. 10)
, 遍历1 <= i < 10)
ok,开始正题:
1)遍历
#include<iostream>using namespace std;int main(void) { int number[5] = {3, 2, 1, 5, 4}; cout << "Output :" << endl; for(int x : number) //warning: 此处为 :, 而不是 ; cout << x << ' '; cout << endl; return 0;}for循环的含义为:变量x在数组number中遍历,且x代表数组中被指向的元素
Test :
2)修改数组元素
#include<iostream>using namespace std;int main(void) { int number[5] = {3, 2, 1, 5, 4}; for(int &x : number) x *= 2; //将数组中每个元素乘2 cout << "Output :" << endl; for(int x : number) cout << x << ' '; cout << endl; return 0;}for循环中定义了`&x`,&表明x为引用变量,即改变x的值可以直接改变数组中对应元素的值。
Test :
3)for循环与初始化列表的结合
#include<iostream>using namespace std;int main(void) { cout << "Output :" << endl; for(int x : {5, 4, 1, 2, 3}) cout << x << ' '; cout << endl; return 0;}x将遍历列表{5, 4, 1, 2, 3}中的值
Test :
本次的笔记(一)暂时到此为止,如有不足会持续更新、添加。
- c 学习笔记 (一)
- C学习笔记(一)
- C ++ 学习笔记(一)
- C学习笔记(一)
- C++&C学习笔记(一)(基础)
- c学习笔记(一)
- 【C++】学习笔记一
- Pro *C/C++学习笔记(一)
- C语言学习笔记(一)
- C语言学习笔记(一)
- C++primer学习笔记(一)
- C++Primer学习笔记(一)
- Object-C学习笔记(一)
- C语言学习笔记(一)
- C和指针 学习笔记(一)
- C语言学习笔记(一)
- c++Templates学习笔记(一)
- Pro *C/C++学习笔记(一)
- C/C++学习之C提高----函数调用模型、指针做函数参数、字符串的基本操作、一级指针内存模型建立
- 第十三篇 elasticsearch的Query DSL搜索语法
- 012-SDK框架之LYWSDKProtocolIOS.mm
- 理解Android Hander、Looper、Message 、MessageQueue、HandlerThread
- Q_OBJECT问题
- C++学习笔记(一)
- tensorflow查看自己的图片数据报错UnicodeEncodeError
- Linux软件安装
- NOIP2017赛前模拟(2017.10.31)考试总结
- virtualbox的虚拟机ip
- c++自学笔记
- python中各种库常用函数记录
- linux命令:rz和sz的使用
- neon加速图像转置