Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- using namespace std;
- int main(){
- // Чи пустой вектор
- vector<string> array;
- // Поток с файла
- fstream myfile;
- // Временная строка для каждой строки файла
- string templine;
- myfile.open("LastName.txt");
- cout << "Reading file contents, aading to array:" << endl;
- // Getline читает поток myfile и кладет одну строку в tempfile,
- // пока не вернёт EOF, т.е. конец файла
- while(getline(myfile, templine))
- {
- array.push_back(templine);
- }
- // Вывод в консоль
- for (string line : array)
- {
- cout << line << endl;
- }
- myfile.close();
- // Наибольшее количество букв в строке и его id в array
- int maxlen = 0, maxlenid;
- for (int i = 0; i < array.size(); i++)
- {
- if (array[i].length() > maxlen)
- {
- maxlen = array[i].length();
- maxlenid = i;
- }
- }
- cout << "Largest last name is \"" << array[maxlenid] << "\"" << endl;
- cout << "Deleting value at " << maxlenid + 1 << " position" << endl;
- // array.erase удаляет значение по индексу и переразмещает массив в памяти.
- // array.begin возвращает интератор ввода-вывода для массива в позиции [0]
- // прибавив к нему maxlenid у нас итератор на нужном месте, удаляем.
- string toWrite = array[maxlenid];
- array.erase(array.begin() + maxlenid);
- ofstream writeFile;
- writeFile.open("Write.txt");
- for (string line : array)
- {
- writeFile << line << endl;
- cout << line << endl;
- }
- writeFile.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement