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 :

这里写图片描述


本次的笔记(一)暂时到此为止,如有不足会持续更新、添加。

原创粉丝点击