Given a text file with the image of integers that must be rewritten in the queue Qu1. In the Qu2 queue, first write the numbers of palindromes, then the numbers, the sum of the digits of which is odd, the remaining numbers to write to the output file. Create a program using OOP methods.

I did what I could, but there were a lot of mistakes ... I ask for your help!

#include "stdafx.h" #include <iostream> #include <conio.h> #include <queue> #include <fstream> #include <algorithm> #include <iterator> using namespace std; unsigned getReverse(unsigned n) { unsigned r = n % 10; while (n /= 10) { r = r * 10 + n % 10; } return r; } bool isPalindrom(const unsigned n) { return n == getReverse(n); } class cQueue { protected: queue<int> p_; public: void IO(); void Del() { while (!p_.empty()) { p_.pop(); } }; }; void cQueue::IO() { ifstream ifile("text.txt"); ofstream ofile("output.txt", ios::trunc); queue<int> qu1 = p_; for_each(istream_iterator<int>(ifile), istream_iterator<int>(), [&qu1](int n) { qu1.push(n); }); cout << "Number of items in the queue: " << qu1.size() << endl; while (!qu1.empty()) { cout << "\nHere they are: " << qu1.front() << '\n'; } queue<int> qu2; copy_if(queue<int>(qu1), queue<int>(), queue<int>(qu2), [&qu1](int n) -> bool { if (isPalindrom(n)) { return n; } else if (n) { // Сумма нечетных чисел ? } else { // В выходной файл ? } }); cout << "Number of items in the queue: " << qu2.size() << endl; while (!qu2.empty()) { cout << "\nHere they are: " << qu2.front() << '\n'; } } int main() { cout << "Demo Queue OOP" << endl; cQueue queue; queue.IO(); cout << endl; queue.Del(); _getch(); return 0; } 

Error C2675 unary "++": "std :: queue >>" does not define this operator or type conversion is acceptable to the built-in operator \ algorithm 594

C2100 error invalid indirect reference \ algorithm 596

Error C2100 invalid indirect reference \ algorithm 598

Error C2675 unary "++": "std :: queue >>" does not define this operator or type conversion is acceptable to the built-in operator \ algorithm 599

Error C4996 'std :: copy_if :: _ Unchecked_iterators :: _ Deprecate': Call to 'std :: copy_if' To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. Visual C ++ 'Checked Iterators' \ algorithm 589

  • So do you write gradually, dealing with errors as they appear. - VTT
  • @VTT all errors occur due to copy_if(...) - MiT
  • Well, the first three parameters must be iterators. The directory is sometimes worth a look. - VTT
  • the queue does not support iterators, and is not an iterator (as in your code). Need to do without STL algorithms - AR Hovsepyan
  • @ARHovsepyan and how to do it? - MiT

2 answers 2

 #include <iostream> #include <fstream> #include <algorithm> #include <queue> using namespace std; struct IsPoli { bool operator()(const int i) { string s = to_string(i), t = s; std::reverse(t.begin(), t.end()); return s == t; } }; struct CheckSum { bool operator()(int n) { int count{}; while(n) { if(n & 1) ++count; n /= 10; } return (count & 1); } }; int main() { ifstream ifile("text.txt"); ofstream ofile("output.txt"); queue<int> Qu1, Qu2; for_each(istream_iterator<int>(ifile), istream_iterator<int>(), [&](const int& i) { Qu1.push(i);}); IsPoli p; CheckSum cmp; int k{}; vector<int> v; while(!Qu1.empty()) { k = Qu1.front(); if (!p(k) && !cmp(k)) ofile << k << ' '; else if (p(k)){ Qu2.push(k); } else v.emplace_back(k); Qu1.pop(); } for (const int& i :v) Qu2.push(i); // дальше можно показать содержимое второй очереди while(!Qu2.empty()) { cout << endl << Qu2.front(); Qu2.pop(); } return 0; } 
  • Please explain how CheckSum works? Did not quite understand ... - MiT
  • one
    n & 1 gives true if the first bit is 1, i.e. if n is an odd number. count stores the number of odd numbers (you can write n% 2 instead of this expression), and if their number is odd, then the sum of these numbers will be odd ... Everything is clear? .. The code as a whole can still be optimized (I wrote it without thinking twice ) - AR Hovsepyan
  • Yes, all thanks, understood! - MiT

Your copy_if for queues to do is necessary. Or do not use queues.

 std::queue<int> input; ... std::queue<int> result; std::queue<int> inputcopy(input); while(not input_copy.empty()){ if(...) result.push(input_copy.front()); input_copy.pop();}