-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
46 lines (44 loc) · 1.25 KB
/
main.cpp
File metadata and controls
46 lines (44 loc) · 1.25 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <windows.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;
vector<vector<string>> arr; //Создаем двумерный массив строк
ifstream in("test.txt"); // Читаем файл
void readFile() {
string line;
if (in.is_open()){
while(getline(in, line)) {
arr.push_back({{line[0]}, line.substr(2)}); // Добавляем в массив: 1 символ строки - "Ключ", 3 символ - "Значение"
}
}
}
void sortLines() {
sort(arr.begin(), arr.end()); // Сортируем массив
}
void printLines() {
for (auto& row : arr) { //Выводим "значения" по "ключам" после сортировки
cout << row[1] << endl;
}}
void writeFile() {
stringstream sstream;
for (int i = 0; i < arr.size(); i++) {
sstream << arr[i][0] << " " << arr[i][1] << endl;
}
ofstream out_file("output.txt", ios::binary);
out_file.imbue(locale(""));
string data = sstream.str();
out_file.write(data.c_str(), data.size());
out_file.close();
}
int main()
{
SetConsoleOutputCP(CP_UTF8);
readFile();
sortLines();
printLines();
writeFile();
return 0;
}