RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 837489
Accepted
MiT
MiT
Asked:2020-06-04 19:49:42 +0000 UTC2020-06-04 19:49:42 +0000 UTC 2020-06-04 19:49:42 +0000 UTC

使用队列和 OOP (C++)

  • 772

给定一个文本文件,其中包含需要重写到队列 Qu1 的整数图像。在队列 Qu2 中,首先写入回文数,然后是数字和为奇数的数,将其余数写入输出文件。使用 OOP 方法创建程序。

我做了我能做的,但出现了很多错误......我请求你的帮助!

#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;
}

错误 C2675 一元“++”:“std::queue>>”未定义此运算符或转换为内置 \algorithm 运算符可接受的类型 594

错误 C2100 无效间接\算法 596

错误 C2100 无效间接\算法 598

错误 C2675 一元 '++': 'std::queue>>' 未定义此运算符或转换为内置 \algorithm 运算符 599 可接受的类型

错误 C4996 'std::copy_if::_Unchecked_iterators::_Deprecate': 使用可能不安全的参数调用 'std::copy_if' - 此调用依赖调用者检查传递的值是否正确。要禁用此警告,请使用 -D_SCL_SECURE_NO_WARNINGS。请参阅有关如何使用 Visual C++ 'Checked Iterators' \algorithm 589 的文档

c++
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    AR Hovsepyan
    2020-06-04T22:14:32Z2020-06-04T22:14:32Z
    #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;
    }
    
    • 1
  2. AlexGlebe
    2020-06-04T20:55:44Z2020-06-04T20:55:44Z

    copy_if您需要为队列制作自己的。或者不使用队列。

    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();}
    
    • 0

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5