【问题标题】:How to remove first string element stored in array in a queue without STL?如何在没有 STL 的情况下删除队列中存储在数组中的第一个字符串元素?
【发布时间】:2018-02-10 02:13:49
【问题描述】:

我有一个删除字符串数组的第一个元素的代码。但是它删除了最后输入的元素。如何在不使用 STL 的情况下删除队列中的第一个元素以及如何将队列重置为空。这里是我的类声明为 Queue.h 文件,QueueImpl.h 作为其模板文件。您必须包含在 Queueimpl.h 文件中实现的所有函数,然后将其包含在 main 中。我已成功从队列中删除了该元素,我可以看到打印队列但搜索队列时,删除的元素仍然存在,为什么会这样

     Queue.h
        template <class Type>
        class Queue
        {

        private:
            int counter;
            int Queue_size;
            Type* Contents;
            int Front, Back;
            int items_in_queue = 0;

        public:
            Queue(int queue_size = 10);
            ~Queue();
            bool Empty() const;
            bool Full() const;
            void Remove();
            int Add(const Type& new_element);
            int QueueSize();
            Type front();
            int search(string &element,int numElm);
            void clear();
            bool IsDigitsOnly(string &strn);

        };

        #endif

         QueueImpl.h

                  #pragma once
#ifndef QUEUETEMPLATE_H
#define QUEUETEMPLATE_H
#include<string>
#include "queue.h"
const int MAX_SIZE = 10;
// Constructor
int counter = 0;



template<class Type>
Queue<Type>::Queue(int queue_size) :
    Queue_size(queue_size),
    Contents(new Type[queue_size + 1]),
    Front(0), Back(0)
{}

// Destructor
template<class Type>
Queue<Type> :: ~Queue()
{
    delete[] Contents;
}

// Tests
template<class Type>
void Queue<Type>::clear()
{
    while (!Empty()) {
        Front = Back = -1;


            }





}


template<class Type>
bool Queue<Type>::Empty() const
{
    return (Front == Back) ? true : false;
}
template<class Type>
bool Queue<Type>::Full() const
{
    return ((1 + Back) % (Queue_size + 1) == Front) ? true : false;
}

/
}








#endif

【问题讨论】:

  • 请通过完整的示例。你的这段代码是不可编译的,函数Remove声明无效。
  • 请将模板文件存储在.h扩展名中并包含在main中
  • 为什么文件底部有一个e?此外,您的代码格式也很糟糕。
  • 对不起,我现在改了...
  • 这不会编译。发布一个完整的程序,而不是我们必须组装的 .h 文件和片段。阅读:minimal reproducible example

标签: c++ arrays string queue


【解决方案1】:

好的,现在已经足够了。我不想批评你的代码风格。如果您能演示一个使用示例,那就更好了。没有它,我假设 Type 是 std::string。为了简化,我删除了&lt;Type&gt;。开始吧。

  1. IsDigitsOnly 与您的班级无关。最好把它移到一个单独的函数中。
  2. 如果需要 queue_size,为什么需要 queue_size + 1 数组中的元素 Contents
  3. 构造函数Queue::Queue 初始化一个空队列对象,将FrontBack 置零。应该在 Queue::clear: Front = Back = 0 而不是 Front = Back = -1 中完成。
  4. 只需 return Front == Back 而不是 return (Front == Back) ? true : false
  5. Queue::FullQueue::Empty 相同。他们都应该检查counter
  6. Queue::Remove:条件if (Front == Back) 始终为false,因为相同的条件在if (Empty()) 中。最后的else 是正确的,因为您使用的是正确的Queue_size。你必须在那里counter--
  7. Queue::Add 中使用了错误的Queue_size + 1
  8. Queue::search 不使用 FrontBack 因此这是错误的。见下文。

到此为止吧。我希望你能够自己前进。

5 和 8 的新手。

template<class Type>
bool Queue<Type>::Empty() const
{
    return counter == 0;
}

template<class Type>
bool Queue<Type>::Full() const
{
    return counter == Queue_size;
}

template<class Type>
int Queue<Type>::search(const Type& element, int numElm)
{
    if (numElm < Front || numElm >= Back)
        return -1;
    // Used as a subscript to search array
    for (int index = numElm; index != Back; index = (index + 1) % Queue_size)
    {
        if (Contents[index] == element) // If the value is found 
            return index;
    }
    return -1;
}

【讨论】:

  • 谢谢。但是如何清除队列的内容?
  • 您不需要清除内容。从BackFront 之前的单元格被认为是空闲的。如果您希望更准确地了解内存使用情况,请在Queue::Empty 中将Type() 分配给Front 在其增量之前指向的单元格。
  • 谢谢。我已经成功从队列中删除了元素,我可以看到打印队列时,但在搜索时,删除的元素仍然存在,为什么会这样
  • 这绝对是因为你没有在那里使用FrontBack。请思考并正确行事。我不会为你做全部的工作。特别是你没有投票给我的答案,这完全涵盖了你最初的答案。
  • 对不起。我的任务很忙。请帮帮我。这只是一件小事。请帮我在动态队列中搜索
猜你喜欢
  • 2014-03-04
  • 1970-01-01
  • 2021-12-28
  • 2014-11-10
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 2018-03-02
  • 2011-03-11
相关资源
最近更新 更多