Vektordagi maksimal elementdan kamroq barcha elementlarni olib tashlash funksiyasi


Download 108.02 Kb.
Sana29.04.2023
Hajmi108.02 Kb.
#1401635



#include
#include
#include
#include

using namespace std;

// Vektordagi maksimal elementdan kamroq barcha elementlarni olib tashlash funksiyasi
void remove_elements_less_than_max(vector& v) {
if (v.empty()) {
return;
}
auto max_it = max_element(v.begin(), v.end());
v.erase(v.begin(), find_if_not(v.begin(), v.end(), [max_val=*max_it](int x) { return x < max_val; }));
}
// Fayldan butun sonlarni o'qish va ularni vektorda saqlash funksiyasi
void read_from_file(vector& v, const string& filename) {
ifstream fin(filename);
if (!fin) {
cerr << "Xato: kirish faylini ochib bo'lmadi " << filename << endl;
exit(1);
}
int x;
while (fin >> x) {
v.push_back(x);
}
fin.close();
}

// Vektordan faylga butun sonlarni yozish funksiyasi


void write_to_file(const vector& v, const string& filename) {
ofstream fout(filename);
if (!fout) {
cerr << "Xato: chiqish faylini ochib bo'lmadi " << filename << endl;
exit(1);
}
for (int x : v) {
fout << x << endl;
}
fout.close();
}

int main() {


vector v;
read_from_file(v, "input.txt");
cout<<"Bu yerda input fildedagi malumotlarni o'qiydi va uni ustida amallar bajarib output filega joylaydi\n";
remove_elements_less_than_max(v);
write_to_file(v, "output.txt");
return 0;
}

Download 108.02 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling