十八、常用库函数
1. <fstream>
在C++中,文件读写操作主要通过三个类进行:ifstream
用于从文件读取数据,ofstream
用于向文件写入数据,以及fstream
可以同时进行读写操作。这些类都定义在头文件<fstream>
中。
打开文件
在进行文件读写操作之前,首先需要打开文件。这可以通过在类的对象创建时指定文件名,或者使用对象的open
成员函数来完成。
ifstream infile("input.txt"); // 创建时打开文件
ofstream outfile;
outfile.open("output.txt"); // 使用open函数打开文件
在打开文件时,还可以指定打开模式,例如ios::in
(只读),ios::out
(只写),ios::binary
(二进制模式),等等。
读取文件
文件读取可以通过多种方式完成,包括使用>>
运算符,get
,getline
等函数。getline
是读取一行文本非常常用的方法。
string line;
while (getline(infile, line)) {
// 处理每行数据
}
写入文件
向文件写入数据通常使用<<
运算符,也可以使用write
函数(对于二进制文件)。
outfile << "Hello, World!" << endl;
关闭文件
完成文件操作后,应当关闭文件。这可以通过显式调用close
方法,或者通过销毁文件流对象自动完成。
infile.close();
outfile.close();
示例:文件复制
以下是使用ifstream
和ofstream
进行文件复制的简单示例。
#include <fstream>
#include <iostream>
int main() {
ifstream src("source.txt", ios::binary); // 以二进制模式打开源文件
ofstream dest("destination.txt", ios::binary); // 以二进制模式打开目标文件
if (!src || !dest) {
cerr << "Error opening files!" << endl;
return 1;
}
dest << src.rdbuf(); // 将源文件内容复制到目标文件
src.close();
dest.close();
return 0;
}
这个程序将"source.txt"的内容复制到"destination.txt"。使用了rdbuf
函数,这是一种简单有效的将整个文件内容复制到另一个文件的方法。
2. <sstream>
- 用于字符串流操作。
- 包含
istringstream
、ostringstream
、stringstream
等类。
3. <string>
修改操作
string& operator+=(const string& str)
:将str
添加到当前字符串的末尾。void push_back(char c)
:在字符串的末尾添加一个字符c
。string& insert(size_type pos, const string& str)
:在pos
位置插入字符串str
。string& erase(size_type pos = 0, size_type len = npos)
:从pos
位置开始,删除最多len
个字符。#include <string> string str = "Hello, World! Welcome to C++ programming."; str.erase(5, 7); // 从位置5开始删除7个字符(包括逗号和空格)
void clear()
:清除字符串内容,等同于erase()
。
查询操作
char& front()
:返回字符串的第一个字符。char& back()
:返回字符串的最后一个字符。
搜索和比较
int compare(const string& str) const
:比较当前字符串和str
。
按字典顺序比较字符串内容,并根据比较结果返回整数。具体来说:- 如果当前字符串小于参数字符串
str
,返回值小于0。 - 如果两个字符串相等,返回值为0。
- 如果当前字符串大于参数字符串
str
,返回值大于0。
#include <iostream>
#include <string>
int main() {
string str1 = "apple";
string str2 = "banana";
string str3 = "apple";
string str4 = "Apple"; // 注意首字母大写
if (str1.compare(str2) < 0) {
cout << str1 << " is less than " << str2 << endl;
}
if (str1.compare(str3) == 0) {
cout << str1 << " is equal to " << str3 << endl;
}
if (str1.compare(str4) > 0) {
cout << str1 << " is greater than " << str4 << endl;
}
return 0;
}
size_type find(const string& str, size_type pos = 0) const
:从pos
开始搜索字符串str
,返回第一个匹配的位置。size_type rfind(const string& str, size_type pos = npos) const
:从pos
开始反向搜索字符串str
,返回第一个匹配的位置。
字符串操作
string substr(size_type pos = 0, size_type len = npos) const
:返回一个从pos
开始、长度为len
的子字符串。
string text = "Hello, World!";
string sub = text.substr(7, 5); // 从位置6开始提取5个字符的子字符串
cout << sub << endl; // 输出: World
数值转换
stoi
,stol
,stoul
,stoll
,stoull
,stof
,stod
,stold
:这些函数用于将字符串转换为相应的数值类型。
string strInt = "100";
string strLongLong = "400000000000";
string strFloat = "6.25";
string strDouble = "7.125";
// 将字符串转换为不同的数值类型
int numberInt = stoi(strInt);
long long numberLongLong = stoll(strLongLong);
float numberFloat = stof(strFloat);
double numberDouble = stod(strDouble);
4. <algorithm>
- merge算法用于合并两个已排序的序列,结果是一个新的、同样排序的序列。
vector<int> v1 = {1, 3, 5};
vector<int> v2 = {2, 4, 6};
vector<int> v_merged(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v_merged.begin());
- binary_search算法用于检查一个已排序的序列中是否包含某个特定的元素。它使用二分查找算法进行搜索,因此运行时间是对数级的。
vector<int> v = {1, 2, 3, 4, 5};
if (binary_search(v.begin(), v.end(), 3)) {
cout << "Found the number 3." << endl;
}
- lower_bound算法用于在一个已排序的序列中找到第一个不小于给定值的元素的位置。如果序列中所有元素都小于给定值,该函数返回last。(upper_bound)
vector<int> v = {1, 2, 3, 4, 5};
auto lower = std::lower_bound(v.begin(), v.end(), 3);
cout << "The first number not less than 3 is at position: " << (lower - v.begin()) << std::endl;
5. <cmath>
函数 | 描述 |
---|---|
abs(x) | 计算x 的绝对值。 |
pow(x, y) | 计算x 的y 次幂。 |
sqrt(x) | 计算x 的平方根。 |
cbrt(x) | 计算x 的立方根。 |
hypot(x, y) | 计算直角三角形的斜边长度。 |
exp(x) | 计算自然指数函数e^x 。 |
log(x) | 计算x 的自然对数。 |
log10(x) | 计算以10为底的x 的对数。 |
log2(x) | 计算以2为底的x 的对数。 |
sin(x) | 计算x 的正弦值。 |
cos(x) | 计算x 的余弦值。 |
tan(x) | 计算x 的正切值。 |
asin(x) | 计算x 的反正弦值。 |
acos(x) | 计算x 的反余弦值。 |
atan(x) | 计算x 的反正切值。 |
atan2(y, x) | 根据y/x 的值计算反正切值,考虑了象限。 |
sinh(x) | 计算x 的双曲正弦值。 |
cosh(x) | 计算x 的双曲余弦值。 |
tanh(x) | 计算x 的双曲正切值。 |
ceil(x) | 计算x 的向上取整值。 |
floor(x) | 计算x 的向下取整值。 |
round(x) | 计算x 最接近的整数值,四舍五入。 |
lround(x) | 同round ,返回long int 类型。 |
llround(x) | 同round ,返回long long int 类型。 |
trunc(x) | 返回x 的整数部分,去掉小数。 |
6. <ctime>
- 包括获取当前时间的
time
函数。
time_t now = time(nullptr); // 或者简写为 time(0);
srand(time(nullptr)); // 使用当前时间作为种子
int randomNumber = rand(); // 生成一个随机数