jeudi 4 mars 2021

C++ Data structures implementing member functions

So my professor provided alot of code that basically to me was a complete mystery. Having gone from C++ basics to just being thrown already made templates doesnt help. My question is could someone explain some of this code better with comments. And also a basic format on how to do add_item and i think i can figure out the rest.

The actual assignment is listed in the header, i need to implement an add_item, get_item, remove_item, empty but for some reason i feel like i know nothing after seeing all this.

Header file.hpp

# ifndef _CONTAINER_HPP
# define _CONTAINER_HPP
# include <iostream >
# include <string >
template <class T, int N>
class Container
{
  public :
    using value_type = T; //T = Type_name
    void add_item (T item ); // output container full , if add_item cannot add %10 T get_item (int index ); // throw a string if index out of bounds %10 points
    void remove_item (T item ); // remove first occurrence of item %10 points
    bool empty (); // check of Container is empty %5 points
    void clear (); // clear all contents , assign value_type
    constexpr int size (); // return current number of elements in container
    T* begin ();
    T* end ();

  private :
    int _size = 0;
    T container [N];
}; // Container interface

/* Sample Template member function definitions */
template <class T, int N>
constexpr int Container <T, N >:: size (){ return _size ;}

template <class T, int N>
void Container <T, N >:: clear (){
  for (int i = 0; i < _size ; i++)
    container [i] = T(); // or value_type ();
  _size = 0;
}

template <class T, int N>
  T* Container <T, N >:: begin (){ return _size ? & container [0] : nullptr ;}

template <class T, int N>
  T* Container <T, N >:: end (){ return begin () + _size ;}
//
// TODO : implement add_item , get_item , remove_item , empty
//

# endif

Core file.cpp

//Example tests of the Container type. Place in a file named A01.cpp
# include <iostream >
# include <string >
# include "Container.hpp"

int main ()
{
  std :: cout << "\n******************* Test 1*******************\n";
  std :: cout << " Testing Container <std :: string ,5 >\n";
  Container <std :: string ,5> container_of_strings ;

  std :: cout << " Testing Container <std :: string ,5 >:: add_items () {Green , Red , Black }\n";
    container_of_strings.add_item ("Green ");
    container_of_strings.add_item ("Red");
    container_of_strings.add_item ("Black ");

  std :: cout << " Testing Container <std :: string ,5 >:: range -based -for - loop ()\n";
    //Question: What is auto keyword in C++?
    //Answer: it is used for type deduction
    /*
    auto x = 2;
    auto y = 20.5;
    cout << typeid(x).name() << endl; //reads i(int)
    cout << typeid(y).name() << endl; //reads d(double)
    */
    for ( auto & str : container_of_strings )
      std :: cout << str << " ";

  std :: cout << "\nTesting Container <std :: string ,5 >:: remove_item (Red )\n";
    container_of_strings.remove_item ("Red");

  std :: cout << " Testing Container <std :: string ,5 >:: range -based -for - loop ()\n";
    for ( auto & str : container_of_strings )
      std :: cout << str << " ";

  std :: cout << "\nTesting Container <std :: string ,5 >:: size ()\n";
  std :: cout << " container_of_strings . size ()=" << container_of_strings.size() << "\n";

  std :: cout << "\n******************* Test 2*******************\n";
  std :: cout << " Testing Container <int ,10 >:: add_item (){0 ,2 ,4 ,... ,64 ,81}\n";
    Container <int ,10 > container_of_ints ;
      for (int i = 0; i < 10; i++)
        container_of_ints.add_item(i * i);

  std :: cout << " Testing Container <int ,10 >:: range -based -for - loop \n";
    for ( auto num : container_of_ints )
      std :: cout << num << " ";

  std :: cout << "\nTesting Container <int ,10 >:: remove_item (16)\n";
    container_of_ints.remove_item(16);

  std :: cout << " Testing Container <int ,10 >:: range -based -for - loop \n";
    for ( auto num : container_of_ints )
      std :: cout << num << " ";

  std :: cout << "\nTesting Container <int ,10 >:: clear ()\n";
    container_of_ints.clear();

  std :: cout << " Testing Container <int ,10 >:: empty ()\n";
  std :: cout << " container_of_ints . empty () is " << ( container_of_ints.empty()) ? " True " : "False";

  std :: cout << "\n ***** Test completed , enter any key to exit *******\n";
    char s;
    std :: cin >> s;
    return 0;
}



/*
Example output from tests:
*******************Test 1*******************
Testing Container<std::string,5>
Testing Container<std::string,5>::add_items() {Green, Red, Black}
Testing Container<std::string,5>::range-based-for-loop()
Green Red Black
Testing Container<std::string,5>::remove_item(Red)
Testing Container<std::string,5>::range-based-for-loop()
Green Black
Testing Container<std::string,5>::size()
container_of_strings.size()=2
*******************Test 2*******************
Testing Container<int,10>::add_item(){0,2,4,...,64,81}
Testing Container<int,10>::range-based-for-loop
0 1 4 9 16 25 36 49 64 81
Testing Container<int,10>::remove_item(16)
Testing Container<int,10>::range-based-for-loop
0 1 4 9 25 36 49 64 81
Testing Container<int,10>::clear()
Testing Container<int,10>::empty()
container_of_ints.empty() is True
*/

I get the concept of adding and removing elements but i cant wrap my head around the container or .hpp code.

Aucun commentaire:

Enregistrer un commentaire