Skip to main content
deleted 1 character in body
Source Link
mdfst13
  • 22.5k
  • 6
  • 34
  • 70

I assume that you are familiar with std::queue and std::deque and wrote this class as a exercise.

There are a few mainly aesthetic points that you could improve:

  • int size() is better if returning a std::size_t, which is the underlayingunderlying type returned by vector::size().

  • Keep parameter names in function prototypes, it helps self-documenting the code.

  • T peek() doesn't change any internal state, so it should be const too.

  • operator[] should be provided with two overloads, const and non-const:

      T& operator[](std::size_t index);
      const T& operator[](std::size_t index) const;
    

Otherwise you won't be able to access data in a const Queue<T>. The same thing applies to begin()/end(). You will have to provide both const and non-const versions. This is purely boilerplate code, but C++ doesn't offer a better solution.

  • This excessively long name: typename std::vector <T>::iterator should be replaced by a typedef or using alias:

      using iterator = std::vector<T>::iterator;
    
      // Then you simply use `iterator` now:
      iterator begin() { ... }
    
  • Shouldn't you provide a constructor that takes no arguments? It is very likely that you will want to declare an empty queue at some point. A default constructor should also do:

      Queue() = default;
    

I assume that you are familiar with std::queue and std::deque and wrote this class as a exercise.

There are a few mainly aesthetic points that you could improve:

  • int size() is better if returning a std::size_t, which is the underlaying type returned by vector::size().

  • Keep parameter names in function prototypes, it helps self-documenting the code.

  • T peek() doesn't change any internal state, so it should be const too.

  • operator[] should be provided with two overloads, const and non-const:

      T& operator[](std::size_t index);
      const T& operator[](std::size_t index) const;
    

Otherwise you won't be able to access data in a const Queue<T>. The same thing applies to begin()/end(). You will have to provide both const and non-const versions. This is purely boilerplate code, but C++ doesn't offer a better solution.

  • This excessively long name: typename std::vector <T>::iterator should be replaced by a typedef or using alias:

      using iterator = std::vector<T>::iterator;
    
      // Then you simply use `iterator` now:
      iterator begin() { ... }
    
  • Shouldn't you provide a constructor that takes no arguments? It is very likely that you will want to declare an empty queue at some point. A default constructor should also do:

      Queue() = default;
    

I assume that you are familiar with std::queue and std::deque and wrote this class as a exercise.

There are a few mainly aesthetic points that you could improve:

  • int size() is better if returning a std::size_t, which is the underlying type returned by vector::size().

  • Keep parameter names in function prototypes, it helps self-documenting the code.

  • T peek() doesn't change any internal state, so it should be const too.

  • operator[] should be provided with two overloads, const and non-const:

      T& operator[](std::size_t index);
      const T& operator[](std::size_t index) const;
    

Otherwise you won't be able to access data in a const Queue<T>. The same thing applies to begin()/end(). You will have to provide both const and non-const versions. This is purely boilerplate code, but C++ doesn't offer a better solution.

  • This excessively long name: typename std::vector <T>::iterator should be replaced by a typedef or using alias:

      using iterator = std::vector<T>::iterator;
    
      // Then you simply use `iterator` now:
      iterator begin() { ... }
    
  • Shouldn't you provide a constructor that takes no arguments? It is very likely that you will want to declare an empty queue at some point. A default constructor should also do:

      Queue() = default;
    
Source Link
glampert
  • 17.3k
  • 4
  • 31
  • 89

I assume that you are familiar with std::queue and std::deque and wrote this class as a exercise.

There are a few mainly aesthetic points that you could improve:

  • int size() is better if returning a std::size_t, which is the underlaying type returned by vector::size().

  • Keep parameter names in function prototypes, it helps self-documenting the code.

  • T peek() doesn't change any internal state, so it should be const too.

  • operator[] should be provided with two overloads, const and non-const:

      T& operator[](std::size_t index);
      const T& operator[](std::size_t index) const;
    

Otherwise you won't be able to access data in a const Queue<T>. The same thing applies to begin()/end(). You will have to provide both const and non-const versions. This is purely boilerplate code, but C++ doesn't offer a better solution.

  • This excessively long name: typename std::vector <T>::iterator should be replaced by a typedef or using alias:

      using iterator = std::vector<T>::iterator;
    
      // Then you simply use `iterator` now:
      iterator begin() { ... }
    
  • Shouldn't you provide a constructor that takes no arguments? It is very likely that you will want to declare an empty queue at some point. A default constructor should also do:

      Queue() = default;