io操作

1.int输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//int 型输入
int a = 0;

while (std::cin >> a, !std::cin.eof()) {
if (std::cin.bad()) {
throw std::runtime_error("cin is corruted");
}
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "data format error,pleace try again" << std::endl;
continue;
}
std::cout << a << std::endl;
}

std::cout << "process complete" << std::endl;

2.string输入

1
2
3
4
5
6
7
8
9
10
11
//string 型输入
std::string str;

while (std::getline(std::cin, str), !std::cin.eof()) {
if (std::cin.bad()) {
throw std::runtime_error("cin is corruted");
}
std::cout << str << std::endl;
}

std::cout << "process complete" << std::endl;

文件操作

1.读文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//文件存储在操作系统内核区
std::string fileName;
std::string fileContent;

while (std::cin>>fileName, !std::cin.eof()) {
if (std::cin.bad()) {
throw std::runtime_error("cin is corruted");
}

//文件读取
std::ifstream ifs(fileName);
if (ifs.is_open()) {
while (std::getline(ifs, fileContent)) {
std::cout << fileContent << std::endl;
}
if (ifs.bad()) {
throw std::runtime_error("ifs is corruted");
}
ifs.close();
}
else {
ifs.clear();
ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "file not exit,pleace try again" << std::endl;
continue;
}

}

std::cout << "process complete" << std::endl;

2.清除缓冲区

1
ifs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

  作用是为了释放由操作系统分配的用于文件操作的缓冲区资源,以避免内存泄漏。

  在文件操作过程中,操作系统通常会为每个打开的文件分配一些内存缓冲区,用于提高文件访问效率。这些缓冲区可能用于读取或写入文件内容,或者用于暂存文件操作的结果。如果文件操作失败,那么这些缓冲区中的数据可能是不完整的,或者可能包含了未写入文件的内容。

  如果不进行缓冲区清除,这些缓冲区可能会保留下来,即使文件操作失败,这些缓冲区仍然占用内存资源。如果应用程序打开了大量的文件,且每个文件都分配了缓冲区,那么这些缓冲区可能会累积起来,导致内存使用量增加,甚至可能导致内存泄漏。

  因此,缓冲区清除是非常重要的,可以确保:

  1.释放不完整的缓冲区数据,避免内存泄漏。

  2.释放未写入文件的缓冲区,防止数据丢失。

  3.确保后续的文件操作不会受到之前失败操作的影响。

string流操作

1.string to int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//string to int
std::string str("127");

std::stringstream strStream(str);

int i = 0;
strStream >> i;

if (strStream.bad()) {
throw std::runtime_error("strStream is corruted");
}
else if (std::cin.fail()) {
strStream.clear();
strStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "string format error" << std::endl;
}
else {
std::cout << i << std::endl;
}

std::cout << "process complete" << std::endl;

2.int to string

1
2
3
4
5
6
7
8
9
10
//int to string
int num = 100;
std::stringstream strStream;
strStream << num << std::endl;
if (strStream.bad()) {
throw std::runtime_error("strStream is corruted");
}
else {
std::cout << strStream.str() << std::endl;
}

3.分割字符串

1
2
3
4
5
6
7
8
9
10
//切分字符串
std::string srcString("hello world i love you");
std::stringstream strStream(srcString);
std::string destString;
while (strStream >> destString) {
std::cout << destString << std::endl;
}
if (strStream.bad()) {
throw std::runtime_error("strStream is corruted");
}

多线程操作

1.多线程基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<mutex>
#include<thread>
#include<atomic>

//std::mutex mut;

//或者
//原子操作,不可被分割,即不可被多个线程同时操作
//数据量大时毫无优势
std::atomic<int> data;

void helloworld(){
//std::lock_guard<std::mutex> lg(mut); //互斥锁,构造时上锁,析构时解锁
//需要在一定的作用域内实现,不可没有边界
//尽量覆盖少的代码
std::cout << "hello world" << std::endl;
}


int main()
{
//数据保护,不同线程对同一数据操作会出现不可知错误,
// 因此需要一些手段:互斥锁、原子操作来进行数据保护
std::thread thr(helloworld);
thr.join();

return 0;
}

2.死锁

code

  死锁是计算机操作系统中的一种现象,当多个进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力干涉,这些进程或线程都将无法向前推进。

  通俗来说,死锁就像你和我都在图书馆里找书,我们同时想要借阅对方手中的书。我想要你手中的书,而你想要我手中的书。我们两个都在等待对方先放下手中的书,但是因为没有外力的介入,我们两个人都卡住了,谁也拿不到想要的书籍,这就形成了死锁。

  在操作系统中,死锁通常发生在多个进程或线程同时请求多个资源时。这些资源可以是CPU时间、内存、磁盘I/O、网络资源等。当一个进程或线程请求某个资源时,它可能需要等待另一个进程或线程释放它所持有的资源。如果这个过程无限循环下去,就会形成死锁。

  死锁的问题可以通过一些策略来解决,比如使用资源分配图来检测死锁,或者通过资源分配策略来避免死锁的发生。比如,可以采用资源预分配策略,或者使用信号量来控制资源的访问,从而避免死锁的发生。

3.解决方法

code

  使用adopt_lock使lock_guard只保留解锁的功能。