Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main(){
  8.     // Чи пустой вектор
  9.     vector<string> array;
  10.     // Поток с файла
  11.     fstream myfile;
  12.     // Временная строка для каждой строки файла
  13.     string templine;
  14.     myfile.open("LastName.txt");
  15.     cout << "Reading file contents, aading to array:" << endl;
  16.     // Getline читает поток myfile и кладет одну строку в tempfile,
  17.     // пока не вернёт EOF, т.е. конец файла
  18.     while(getline(myfile, templine))
  19.     {
  20.         array.push_back(templine);
  21.     }
  22.     // Вывод в консоль
  23.     for (string line : array)
  24.     {
  25.         cout << line << endl;
  26.     }
  27.     myfile.close();
  28.     // Наибольшее количество букв в строке и его id в array
  29.     int maxlen = 0, maxlenid;
  30.     for (int i = 0; i < array.size(); i++)
  31.     {
  32.         if (array[i].length() > maxlen)
  33.         {
  34.             maxlen = array[i].length();
  35.             maxlenid = i;
  36.         }
  37.     }
  38.     cout << "Largest last name is \"" << array[maxlenid] << "\"" << endl;
  39.     cout << "Deleting value at " << maxlenid + 1 << " position" << endl;
  40.     // array.erase удаляет значение по индексу и переразмещает массив в памяти.
  41.     // array.begin возвращает интератор ввода-вывода для массива в позиции [0]
  42.     // прибавив к нему maxlenid у нас итератор на нужном месте, удаляем.
  43.     string toWrite = array[maxlenid];
  44.     array.erase(array.begin() + maxlenid);
  45.     ofstream writeFile;
  46.     writeFile.open("Write.txt");
  47.     for (string line : array)
  48.     {
  49.         writeFile << line << endl;
  50.         cout << line << endl;
  51.     }
  52.      writeFile.close();
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement