- 本系列 c++ 学习框架推荐:
- 【零基础一文学C++基础与进阶】.
思维导图:
代码示例:
- 示例1:
/*************************************************
**
**Description: 使用width控制输出宽度
** Author:慕灵阁-wupke
** Time:20221-8
** Versions 10-1.cpp
**
*
***************************************************/
#include <iostream>
using namespace std;
int main() {
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
//cout.fill('*');
for(int i = 0; i < 4; i++) {
cout.width(20); //设置输出的统一宽度 20
cout << values[i] << endl;
}
system("pause");
return 0;
}
- 示例2:
/*************************************************
**
**Description: 使用setw操纵符指定宽度
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-2.cpp
**
*
***************************************************/
#include <iostream>
#include <iomanip> //引入头文件
#include <string>
using namespace std;
int main() {
double values[] = { 1.23, 5.36, 555.7, 3456.24 };
string names[] = { "AZoot", "Jim", "Alm", "Stant" };
for (int i = 0; i < 4; i++)
cout << setw(6) << names[i] << setw(10) << values[i] << endl;
system("pause");
return 0;
}
- 示例3:
/*************************************************
**
**Description: 设置对齐方式
通过使用带参数的setiosflags操纵符来设置左对齐,setiosflags定义在头文件iomanip中。
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-3.cpp
**
*
***************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
string names[] = { "Zoot", "Jimmy", "Al", "Stan" };
for (int i=0;i<4;i++)
cout << setiosflags(ios_base::left) //设置左对齐
<< setw(6) << names[i]
<< resetiosflags(ios_base::right) //右对齐
<< setw(10) << values[i] << endl;
system("pause");
return 0;
}
- 示例4:
/*************************************************
**
**Description: 设置输出精度
• 如果不指定fixed或scientific,精度值表示有效数字位数。
• 如果设置了iosbase::fixed (定点小数)或iosbase::scientific (科学计数法) 精度值表示小数点之后的位数。
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-4.cpp
**
*
***************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
double values[] = { 1.23, 35.36, 653.7, 4358.24 };
string names[] = { "Zoot", "Jimmy", "Al", "Stan" };
cout //<< setiosflags(ios_base::fixed) 设置 定点小数
//<< fixed
<<setprecision(6) // setprecision(6):表示有效数字的位数
<<3466.9768<<endl; // 输出 3466.98
cout//<< setiosflags(ios_base::scientific) 科学计数法
<<scientific
<<setprecision(6)
<<dec<<3466.9768<<endl; // 输出 3.466977e+003
for (int i=0;i<4;i++)
cout << setiosflags(ios_base::left)
<< setw(6) << names[i]
<< resetiosflags(ios_base::left)
<< setw(10) << setprecision(1) << values[i] << endl; // setprecision(1):表示有效数字的位数
system("pause");
return 0;
}
- 示例5:
/*************************************************
**
**Description: 向二进制文件输出
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-5.cpp
**
*
***************************************************/
#include <fstream>
using namespace std;
struct Date {
int mondy, day, year;
};
int main() {
Date dt = { 6, 10, 92 };
ofstream file("date.dat", ios_base::binary); // 打开二进制文件 date.dat
file.write(reinterpret_cast<char *>(&dt), sizeof(dt)); // 写入
file.close(); //关闭文件
/*
file.open("integer", ios_base::binary);
int i=12;
file.write(reinterpret_cast<char *>(&i), sizeof(int));
int j=20;
file.write(reinterpret_cast<char *>(&j), sizeof(int));
int k=0;
file.write(reinterpret_cast<char *>(&k), sizeof(int));
file.close();*/
system("pause");
return 0;
}
- 示例6:
/*************************************************
**
**Description: 字符串输出流示例( ostringstream )
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-6.cpp
**
*
***************************************************/
#include <iostream>
#include <sstream>
#include <typeinfo> // 查看数据类型的方法 头文件
#include <string>
using namespace std;
template <class T> // 定义函数模板:转换为字符串
inline string toString(const T &v) {
ostringstream os; //创建输出字符串流
os << v; //将变量v的值写入字符串流
return os.str(); //返回输出流生成的字符串
}
double a = 3.23 ;
int main() {
string str1 = toString(5); // 数值 类型 转 字符串 类型
cout << str1 << endl;
string str2 = toString(1.2);
cout <<str2 << endl;
cout << typeid( a ).name() << endl; // 查看数据 a 的类型
string str3 = toString(a);
cout << typeid( str3 ).name() << endl; // 查看数据 a 转化后的类型
system("pause");
return 0;
}
- 示例7:
/*************************************************
**
**Description: 输入流 get函数应用举例
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-7.cpp
**
*
***************************************************/
#include <iostream>
using namespace std;
int main() {
char ch;
while ((ch = cin.get()) != EOF) // // EOF为读到结束的文件结束符号(Windows系统键盘ctrl+z)
cout.put(ch);
system("pause");
return 0;
}
- 示例8:
/*************************************************
**
**Description: 输入流函数示例:为输入流指定一个终止字符:
从键盘读入一行带空格的字符串,直到遇到终止字符
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-8.cpp
**
*
***************************************************/
#include <iostream>
#include <string>
using namespace std;
int main() {
string line;
cout << "Type a line terminated by 't' " << endl;
getline(cin, line, 't');
cout << line << endl;
system("pause");
return 0;
}
- 示例9:
/*************************************************
**
**Description: 从文件读一个二进制记录到结构体中
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-9.cpp
**
*
***************************************************/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct SalaryInfo { // 定义的SalaryInfo 结构体
unsigned id;
double salary;
};
int main() {
SalaryInfo employee1 = { 600001, 8000 };
ofstream os("payroll", ios_base::out | ios_base::binary); // 文件输出流对象ofstream os ,
// 当前文件夹下"payroll" 文件名;ios_base::out | ios_base::binary 指定二进制输出方式
os.write(reinterpret_cast<char *>(&employee1), sizeof(employee1));
// reinterpret_cast<char *>(&employee1) 转化为字符指针; employee1为起始地址的文件,全部写入到磁盘文件中
os.close();
ifstream is("payroll", ios_base::in | ios_base::binary);
if (is) {
SalaryInfo employee2;
is.read(reinterpret_cast<char *>(&employee2), sizeof(employee2));
// 从这个字符指针位置开始读,读取sizeof()个字节
cout << employee2.id << " " << employee2.salary << endl;
} else {
cout << "ERROR: Cannot open file 'payroll'." << endl;
}
is.close();
system("pause");
return 0;
}
- 示例10:
/*************************************************
**
**Description: seekg 函数示例: 设置位置指针
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-10.cpp
**
*
***************************************************/
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int values[] = { 3, 7, 0, 5, 4 };
ofstream os("integers", ios_base::out | ios_base::binary); // 构建输出流
os.write(reinterpret_cast<char *>(values), sizeof(values)); // 写入文件
os.close(); // 关闭文件
ifstream is("integers", ios_base::in | ios_base::binary); // 打开文件 : "integers" 文件名
if (is) {
is.seekg(3 * sizeof(int)); // seekg函数,移动读取指针的字节数(位置)
int v;
is.read(reinterpret_cast<char *>(&v), sizeof(int)); // 从指定的位置读取
cout << "The 4th integer in the file 'integers' is " << v << endl;
} else {
cout << "ERROR: Cannot open file 'integers'." << endl;
}
is.close();
system("pause");
return 0;
}
- 示例11:
/*************************************************
**
**Description: 示例:读一个文件并显示出其中0元素的位置
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-11.cpp
**
*
***************************************************/
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// 打开文件 构造 iostream 对象: file ; 与"integers"文件相关联;指定打开模式是二进制输入
ifstream file("integers", ios_base::in | ios_base::binary);
if (file) { // 打开成功
while (file) { //读到文件尾file为0
streampos here = file.tellg(); // 读取下一个数据之前,先获取当前的读取的指针位置
int v;
file.read(reinterpret_cast<char *>(&v), sizeof(int)); // 读取,每次读取sizeof(int)个字节;读取到的字节放到变量 v 中,将v 的地址转换为字符指针
if (file && v == 0) // file 文件没有读取完,且v == 0
cout << "Position " << here << " is 0" << endl;
}
} else {
cout << "ERROR: Cannot open file 'integers'." << endl;
}
file.close(); //读取完,关闭文件
system("pause");
return 0;
}
- 示例12:
/*************************************************
**
**Description: 字符串输入流示例:使用 istringstream 将字符串转换为数值
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-12.cpp
**
*
***************************************************/
#include <iostream>
#include <sstream>
#include <string>
#include<typeinfo>
using namespace std;
template <class T> // 创建函数模板
inline T fromString(const string &str) {
istringstream is(str); //创建输入字符串流
T v;
is >> v; //从输入字符串流中读取变量v
return v; //返回变量v
}
string a = "6";
string b = "5.55";
int main() {
int v1 = fromString<int>("5"); // 套用模板函数
cout << v1 << endl;
double v2 = fromString<double>("1.2");
cout << v2 << endl;
int v3 = fromString<int>(a);
cout << v3 << typeid(v3).name() << endl;
double v4 = fromString<double>(b);
cout << v4 << typeid(v4).name() << endl;
system("pause");
return 0;
}
- 示例13:
/*************************************************
**
**Description: getline 函数示例
• getline 功能是从输入流中读取多个字符,并且允许指定输入终止字符,读取完成后,从读取的内容中删除终止字符。
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-13.cpp
**
*
***************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <locale>
using namespace std;
int main() {
locale loc(".936"); //创建本地化配置方案
wcout.imbue(loc); //为wcout设置编码方案
wifstream in("article.txt"); //创建文件宽输入流,打开文件article.txt
in.imbue(loc); //为in设置编码方案
wstring line; //用来存储一行内容
unsigned number = 0; //记录行号
while (getline(in, line)) {
number++; //行号加1
if (line.find_first_of(L'人') != wstring::npos) //查找“人”字
wcout << number << L": " << line << endl; //输出包含“人”字的行
}
system("pause");
return 0;
}
- 示例14:
/*************************************************
**
**Description: 综合实例:个人银行账户管理程序
** Author:慕灵阁-wupke
** Time:2021-1-8
** Versions :10-14.cpp
**
*
***************************************************/