C++标准输入输出
1. 初识C++程序结构
1.1 main()函数
示例1: 一个简单的C++程序
#include <iostream>
using namespace std;int main() {cout << "你好,世界!" << endl;return 0;
}
示例2: 带有返回值的main()函数
#include <iostream>
using namespace std;int main() {cout << "这个程序返回0。" << endl;return 0;
}
示例3: 带有命令行参数的main()函数
#include <iostream>
using namespace std;int main(int argc, char* argv[]) {cout << "命令行参数的数量: " << argc << endl;return 0;
}
示例4: 在main()函数中调用其他函数
#include <iostream>
using namespace std;void printMessage() {cout << "这是来自另一个函数的消息。" << endl;
}int main() {cout << "调用另一个函数..." << endl;printMessage();return 0;
}
示例5: 在main()函数中使用条件语句
#include <iostream>
using namespace std;int main() {int x = 10;if (x > 5) {cout << "x大于5。" << endl;} else {cout << "x不大于5。" << endl;}return 0;
}
示例6: 在main()函数中使用循环语句
#include <iostream>
using namespace std;int main() {for (int i = 1; i <= 5; i++) {cout << "第" << i << "次迭代" << endl;}return 0;
}
1.2 头文件、代码主体
示例1: 包含单个头文件的C++程序
#include <iostream>
using namespace std;int main() {cout << "这个程序包含了iostream头文件。" << endl;return 0;
}
示例2: 包含多个头文件的C++程序
#include <iostream>
#include <string>
#include <cmath>
using namespace std;int main() {cout << "这个程序包含了多个头文件。" << endl;string message = "你好,世界!";double result = sqrt(16);cout << message << endl;cout << "16的平方根: " << result << endl;return 0;
}
示例3: 使用自定义头文件的C++程序
#include <iostream>
#include "myheader.h"
using namespace std;int main() {cout << "这个程序使用了自定义头文件。" << endl;printCustomMessage();return 0;
}
// myheader.h
#ifndef MYHEADER_H
#define MYHEADER_Hvoid printCustomMessage();#endif
// myheader.cpp
#include <iostream>
#include "myheader.h"
using namespace std;void printCustomMessage() {cout << "这是来自自定义头文件的消息。" << endl;
}
示例4: 使用命名空间的C++程序
#include <iostream>
using namespace std;namespace myNamespace {void printMessage() {cout << "这是来自myNamespace的消息。" << endl;}
}int main() {myNamespace::printMessage();return 0;
}
示例5: 使用条件编译指令的C++程序
#include <iostream>
using namespace std;#define DEBUGint main() {#ifdef DEBUGcout << "调试模式已激活。" << endl;#elsecout << "调试模式未激活。" << endl;#endifreturn 0;
}
示例6: 使用内联函数的C++程序
#include <iostream>
using namespace std;inline int square(int x) {return x * x;
}int main() {int result = square(5);cout << "5的平方: " << result << endl;return 0;
}
1.3 注释的使用
示例1: 使用单行注释的C++程序
#include <iostream>
using namespace std;int main() {// 这是一个单行注释cout << "你好,世界!" << endl;return 0;
}
示例2: 使用多行注释的C++程序
#include <iostream>
using namespace std;int main() {/*这是一个多行注释*/cout << "你好,世界!" << endl;return 0;
}
示例3: 使用注释解释变量的C++程序
#include <iostream>
using namespace std;int main() {int age = 25; // 声明并初始化一个整型变量'age'double height = 1.75; // 声明并初始化一个双精度浮点型变量'height'cout << "年龄: " << age << endl;cout << "身高: " << height << " 米" << endl;return 0;
}
示例4: 使用注释解释函数的C++程序
#include <iostream>
using namespace std;// 计算一个数的平方的函数
int square(int x) {return x * x;
}int main() {int result = square(5);cout << "5的平方: " << result << endl;return 0;
}
示例5: 使用注释临时禁用代码的C++程序
#include <iostream>
using namespace std;int main() {cout << "这行代码会被执行。" << endl;// cout << "这行代码被注释掉了,不会被执行。" << endl;return 0;
}
示例6: 使用注释提供代码概述的C++程序
#include <iostream>
using namespace std;/*这个程序演示了如何使用注释来提供代码概述。它包含一个main()函数,该函数输出一条欢迎消息。
*/int main() {cout << "欢迎使用这个C++程序!" << endl;return 0;
}
2. 变量与数据类型
2.1 整型、浮点型变量
示例1: 整型变量的声明与使用
#include <iostream>
using namespace std;int main() {int age = 25;cout << "年龄: " << age << endl;return 0;
}
示例2: 浮点型变量的声明与使用
#include <iostream>
using namespace std;int main() {double price = 9.99;cout << "价格: " << price << endl;return 0;
}
示例3: 整型和浮点型变量的转换
#include <iostream>
using namespace std;int main() {int x = 10;double y = x; // 隐式转换cout << "x = " << x << endl;cout << "y = " << y << endl;return 0;
}
示例4: 整型变量的溢出
#include <iostream>
using namespace std;int main() {int x = 2147483647; // int型变量的最大值x = x + 1; // 溢出cout << "x = " << x << endl;return 0;
}
示例5: 浮点型变量的精度损失
#include <iostream>
using namespace std;int main() {double x = 1.0 / 3.0;cout << "x = " << x << endl; // 输出结果可能与预期不同return 0;
}
示例6: 使用不同进制表示整数
#include <iostream>
using namespace std;int main() {int decimalNum = 10; // 十进制int octalNum = 012; // 八进制int hexNum = 0xA; // 十六进制cout << "十进制: " << decimalNum << endl;cout << "八进制: " << octalNum << endl;cout << "十六进制: " << hexNum << endl;return 0;
}
2.2 char、string类型
示例1: 字符变量的声明与使用
#include <iostream>
using namespace std;int main() {char grade = 'A';cout << "成绩: " << grade << endl;return 0;
}
示例2: 字符串变量的声明与使用
#include <iostream>
#include <string>
using namespace std;int main() {string message = "你好,世界!";cout << "消息: " << message << endl;return 0;
}
示例3: 字符变量的ASCII码值
#include <iostream>
using namespace std;int main() {char ch = 'A';int asciiValue = ch;cout << "字符 '" << ch << "' 的ASCII码值: " << asciiValue << endl;return 0;
}
示例4: 字符串的连接
#include <iostream>
#include <string>
using namespace std;int main() {string firstName = "John";string lastName = "Doe";string fullName = firstName + " " + lastName;cout << "全名: " << fullName << endl;return 0;
}
示例5: 字符串的长度
#include <iostream>
#include <string>
using namespace std;int main() {string message = "Hello, World!";int length = message.length();cout << "消息长度: " << length << endl;return 0;
}
示例6: 字符串的访问与修改
#include <iostream>
#include <string>
using namespace std;int main() {string message = "Hello";cout << "第一个字符: " << message[0] << endl;message[0] = 'J';cout << "修改后的消息: " << message << endl;return 0;
}
2.3 变量的声明与初始化
示例1: 变量的声明
#include <iostream>
using namespace std;int main() {int x; // 声明整型变量xdouble y; // 声明双精度浮点型变量ychar ch; // 声明字符型变量chstring str; // 声明字符串变量strreturn 0;
}
示例2: 变量的初始化
#include <iostream>
using namespace std;int main() {int x = 10; // 声明并初始化整型变量xdouble y = 3.14; // 声明并初始化双精度浮点型变量ychar ch = 'A'; // 声明并初始化字符型变量chstring str = "Hello"; // 声明并初始化字符串变量strreturn 0;
}
示例3: 多个变量的声明与初始化
#include <iostream>
using namespace std;int main() {int a = 1, b = 2, c = 3; // 同时声明并初始化多个整型变量double x = 1.0, y = 2.0, z = 3.0; // 同时声明并初始化多个双精度浮点型变量return 0;
}
示例4: 变量的默认初始化
#include <iostream>
using namespace std;int main() {int x; // 默认初始化为随机值double y; // 默认初始化为随机值char ch; // 默认初始化为随机值string str; // 默认初始化为空字符串cout << "x = " << x << endl;cout << "y = " << y << endl;cout << "ch = " << ch << endl;cout << "str = " << str << endl;return 0;
}
示例5: 变量的作用域
#include <iostream>
using namespace std;int x = 10; // 全局变量int main() {int x = 20; // 局部变量cout << "局部变量x = " << x << endl;cout << "全局变量x = " << ::x << endl; // 使用`::`访问全局变量return 0;
}
示例6: 变量的生命周期
#include <iostream>
using namespace std;void func() {int x = 10; // 局部变量,只在func()函数内部有效cout << "在func()内部,x = " << x << endl;
}int main() {func();// cout << "在main()内部,x = " << x << endl; // 错误,x在此处不可访问return 0;
}
3. cout输出
3.1 输出整数、小数
示例1: 输出整数
#include <iostream>
using namespace std;int main() {int x = 10;cout << "x = " << x << endl;return 0;
}
示例2: 输出小数
#include <iostream>
using namespace std;int main() {double y = 3.14;cout << "y = " << y << endl;return 0;
}
示例3: 输出表达式的值
#include <iostream>
using namespace std;int main() {int a = 5, b = 3;cout << "a + b = " << a + b << endl;cout << "a - b = " << a - b << endl;cout << "a * b = " << a * b << endl;cout << "a / b = " << a / b << endl;return0;
}
示例4: 输出不同进制的整数
#include <iostream>
using namespace std;int main() {int decimalNum = 10;int octalNum = 012;int hexNum = 0xA;cout << "十进制: " << decimalNum << endl;cout << "八进制: " << oct << octalNum << endl;cout << "十六进制: " << hex << hexNum << endl;return 0;
}
示例5: 输出布尔值
#include <iostream>
using namespace std;int main() {bool flag1 = true;bool flag2 = false;cout << "flag1 = " << flag1 << endl;cout << "flag2 = " << flag2 << endl;cout << "5 > 3 = " << (5 > 3) << endl;cout << "5 < 3 = " << (5 < 3) << endl;return 0;
}
示例6: 输出变量的地址
#include <iostream>
using namespace std;int main() {int x = 10;cout << "变量x的地址: " << &x << endl;double y = 3.14;cout << "变量y的地址: " << &y << endl;return 0;
}
3.2 输出字符、字符串
示例1: 输出字符
#include <iostream>
using namespace std;int main() {char ch = 'A';cout << "ch = " << ch << endl;return 0;
}
示例2: 输出字符串
#include <iostream>
#include <string>
using namespace std;int main() {string str = "Hello, World!";cout << "str = " << str << endl;return 0;
}
示例3: 输出字符串常量
#include <iostream>
using namespace std;int main() {cout << "Hello, C++!" << endl;return 0;
}
示例4: 输出转义字符
#include <iostream>
using namespace std;int main() {cout << "Hello\tWorld!" << endl;cout << "Hello\nWorld!" << endl;cout << "Hello\\World!" << endl;cout << "Hello\"World!" << endl;return 0;
}
示例5: 输出字符串的一部分
#include <iostream>
#include <string>
using namespace std;int main() {string str = "Hello, World!";cout << "str的前5个字符: " << str.substr(0, 5) << endl;cout << "str的最后6个字符: " << str.substr(str.length() - 6) << endl;return 0;
}
示例6: 输出字符的ASCII码值
#include <iostream>
using namespace std;int main() {char ch = 'A';cout << "字符 '" << ch << "' 的ASCII码值: " << (int)ch << endl;return 0;
}
3.3 格式化输出
示例1: 控制输出宽度
#include <iostream>
#include <iomanip>
using namespace std;int main() {int x = 10;cout << "x = " << setw(5) << x << endl;cout << "x = " << setw(5) << setfill('0') << x << endl;return 0;
}
示例2: 控制输出精度
#include <iostream>
#include <iomanip>
using namespace std;int main() {double x = 3.1415926;cout << "x = " << setprecision(4) << x << endl;cout << "x = " << fixed << setprecision(2) << x << endl;return 0;
}
示例3: 左对齐和右对齐
#include <iostream>
#include <iomanip>
using namespace std;int main() {cout << left << setw(10) << "Name" << setw(10) << "Age" << endl;cout << left << setw(10) << "Alice" << setw(10) << 25 << endl;cout << left << setw(10) << "Bob" << setw(10) << 30 << endl;return 0;
}
示例4: 输出布尔值为"true"或"false"
#include <iostream>
#include <iomanip>
using namespace std;int main() {bool flag1 = true;bool flag2 = false;cout << boolalpha;cout << "flag1 = " << flag1 << endl;cout << "flag2 = " << flag2 << endl;return 0;
}
示例5: 输出八进制和十六进制数
#include <iostream>
using namespace std;int main() {int x = 100;cout << "十进制: " << dec << x << endl;cout << "八进制: " << oct << x << endl;cout << "十六进制: " << hex << x << endl;return 0;
}
示例6: 输出正负号
#include <iostream>
using namespace std;int main() {int x = 10;int y = -10;cout << showpos;cout << "x = " << x << endl;cout << "y = " << y << endl;return 0;
}
4. cin输入
4.1 输入整数、小数
示例1: 输入整数
#include <iostream>
using namespace std;int main() {int x;cout << "请输入一个整数: ";cin >> x;cout << "你输入的整数是: " << x << endl;return 0;
}
示例2: 输入小数
#include <iostream>
using namespace std;int main() {double x;cout << "请输入一个小数: ";cin >> x;cout << "你输入的小数是: " << x << endl;return 0;
}
示例3: 同时输入多个整数
#include <iostream>
using namespace std;int main() {int a, b, c;cout << "请输入三个整数,用空格分隔: ";cin >> a >> b >> c;cout << "你输入的三个整数是: " << a << " " << b << " " << c << endl;return 0;
}
示例4: 同时输入多个小数
#include <iostream>
using namespace std;int main() {double x, y, z;cout << "请输入三个小数,用空格分隔: ";cin >> x >> y >> z;cout << "你输入的三个小数是: " << x << " " << y << " " << z << endl;return 0;
}
示例5: 输入整数和小数
#include <iostream>
using namespace std;int main() {int a;double x;cout << "请输入一个整数和一个小数,用空格分隔: ";cin >> a >> x;cout << "你输入的整数是: " << a << endl;cout << "你输入的小数是: " << x << endl;return 0;
}
示例6: 输入表达式的值
#include <iostream>
using namespace std;int main() {int a, b;char op;cout << "请输入一个表达式(如: 1 + 2): ";cin >> a >> op >> b;if (op == '+') {cout << a << " + " << b << " = " << a + b << endl;} else if (op == '-') {cout << a << " - " << b << " = " << a - b << endl;} else if (op == '*') {cout << a << " * " << b << " = " << a * b << endl;} else if (op == '/') {cout << a << " / " << b << " = " << a / b << endl;} else {cout << "不支持的运算符: " << op << endl;}return 0;
}
4.2 输入字符、字符串
示例1: 输入字符
#include <iostream>
using namespace std;int main() {char ch;cout << "请输入一个字符: ";cin >> ch;cout << "你输入的字符是: " << ch << endl;return 0;
}
示例2: 输入字符串
#include <iostream>
#include <string>
using namespace std;int main() {string str;cout << "请输入一个字符串: ";cin >> str;cout << "你输入的字符串是: " << str << endl;return 0;
}
示例3: 输入一行字符串
#include <iostream>
#include <string>
using namespace std;int main() {string line;cout << "请输入一行字符串: ";getline(cin, line);cout << "你输入的字符串是: " << line << endl;return 0;
}
示例4: 同时输入多个字符串
#include <iostream>
#include <string>
using namespace std;int main() {string str1, str2, str3;cout << "请输入三个字符串,用空格分隔: ";cin >> str1 >> str2 >> str3;cout << "你输入的三个字符串是: " << str1 << " " << str2 << " " << str3 << endl;return 0;
}
示例5: 输入字符和字符串
#include <iostream>
#include <string>
using namespace std;int main() {char ch;string str;cout << "请输入一个字符和一个字符串,用空格分隔: ";cin >> ch >> str;cout << "你输入的字符是: " << ch << endl;cout << "你输入的字符串是: " << str << endl;return 0;
}
示例6: 输入指定长度的字符串
#include <iostream>
#include <string>
using namespace std;int main() {char str[10];cout << "请输入一个最多9个字符的字符串: ";cin.getline(str, 10);cout << "你输入的字符串是: " << str << endl;return 0;
}
4.3 多组数据的输入
示例1: 输入多组整数,直到输入0为止
#include <iostream>
using namespace std;int main() {int x;cout << "请输入多个整数,以0结束: " << endl;while (cin >> x && x != 0) {cout << "你输入的整数是: " << x << endl;}return 0;
}
示例2: 输入多组整数,直到文件末尾
#include <iostream>
using namespace std;int main() {int x;cout << "请输入多个整数,以文件末尾结束: " << endl;while (cin >> x) {cout << "你输入的整数是: " << x << endl;}return 0;
}
示例3: 输入多组整数,每组占一行
#include <iostream>
using namespace std;int main() {int x, y;cout << "请输入多组整数,每组占一行,以0 0结束: " << endl;while (cin >> x >> y && (x != 0 || y != 0)) {cout << "你输入的两个整数是: " << x << " " << y << endl;}return 0;
}
示例4: 输入多组字符串,直到输入"end"为止
#include <iostream>
#include <string>
using namespace std;int main() {string str;cout << "请输入多个字符串,以"end"结束: " << endl;while (cin >> str && str != "end") {cout << "你输入的字符串是: " << str << endl;}return 0;
}
示例5: 输入多组字符串,每组占一行
#include <iostream>
#include <string>
using namespace std;int main() {string line;cout << "请输入多组字符串,每组占一行,以空行结束: " << endl;while (getline(cin, line) && line != "") {cout << "你输入的字符串是: " << line << endl;}return 0;
}
示例6: 输入指定组数的整数
#include <iostream>
using namespace std;int main() {int n;cout << "请输入要输入的整数的组数: ";cin >> n;for (int i = 1; i <= n; i++) {int x;cout << "请输入第" << i << "组整数: ";cin >> x;cout << "你输入的第" << i << "组整数是: " << x << endl;}return 0;
}