V
V
Vasya Pupkin2015-11-15 11:38:38
C++ / C#
Vasya Pupkin, 2015-11-15 11:38:38

Validation in class template creation vector?

Task for the laboratory
Class name: Vector and overloaded operation << (collection output to the output stream). Something happened to me, but can you tell me what can be fixed or improved, or offer your own. And advise where to add work with exceptional situations.

#include <iostream>
  #include <string.h>
  #include <vector>
  #include <typeinfo>
  #include <bits/stream_iterator.h>
 
  using namespace std;
 
  template<class T>
  class MainVector {
   private:
    T *m_ptv;
    int m_size,
        m_index;
   public:
    MainVector() : m_size(0), m_index(0), m_ptv(NULL) { }
    MainVector(int);
    ~MainVector() {
      delete[] m_ptv;
    }
    void addOneElem(const T &);
    T *getVecor();
    int getSizeVector();
  };
  template<class T>
  T *MainVector<T>::getVecor() {
    return m_ptv;
  }
 
  template<class T>
  int MainVector<T>::getSizeVector() {
    return m_size;
  }
 
  template<class T>
  ostream &operator<<(ostream &stream, const vector<T> &values) {
    stream << "[ ";
    copy(begin(values), end(values), ostream_iterator<T>(stream, " "));
    stream << ']';
    return stream;
  }
 
  template<class T>
  MainVector<T>::MainVector(int vectorSize) :
      m_size(vectorSize), m_index(0) {
    m_ptv = new T[m_size];
    const type_info &t = typeid(T);
    const char *azaz = t.name();
    for (int i = 0; i < m_size; i++)
      if (!strcmp(azaz, "char")) *(m_ptv + i) = ' ';
      else *(m_ptv + i) = 0;
  }
 
  template<class T>
  void MainVector<T>::addOneElem(const T &t) {
    T *tmp = NULL;
    if (++m_index >= m_size) {
      tmp = m_ptv;
      m_ptv = new T[m_size + 1];
    }
    if (tmp) memcpy(m_ptv, tmp, sizeof(T) * m_size);
    m_ptv[m_size++] = t;
    if (tmp)
      delete[] tmp;
  }
 
  int main() {
    cout << "Hello, Friday 13!" << endl;
 
    vector<int> vectorOne;
 
    vectorOne.push_back(5);
    vectorOne.push_back(13);
    vectorOne.push_back(666);
 
    cout << "Vector #1 : [";
    for (int i = 0; i < vectorOne.size(); ++i) {
      cout << vectorOne[i] << ' ';
    }
    cout << "]";
 
    cout << endl;
 
    vector<char> vectorTwo;
 
    vectorTwo.push_back('D');
    vectorTwo.push_back('e');
    vectorTwo.push_back('v');
    vectorTwo.push_back('i');
    vectorTwo.push_back('l');
 
    cout << "Vector #2 : [";
    for (int i = 0; i < vectorTwo.size(); ++i) {
      cout << vectorTwo[i] << ' ';
    }
    cout << "]";
 
    cout << endl;
 
    cout << "Using overload ostream: " << endl;
    cout << "Vector #1: " << vectorOne << " and Vector #2: " << vectorTwo;
 
    return 0;
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AtomKrieg, 2015-11-15
@AtomKrieg

ostream &operator<<(ostream &stream, const vector<T> &values) {

The second parameter of the function is of the wrong type (should be MainVector< T >)
In order for this construction to work, you need to add the begin(), end() methods and the iterator class.
But it's easier to do it through the usual for(int i=0...) stream << val[i] << _delimiter; (laboratory).
Wherever memory is allocated, you may have a bad_alloc exception. Add MainVector::MainVector(int vectorSize) here and void MainVector::addOneElem(const T &t) here
And in general, wherever you have a function call that is not marked with throw(), you should catch an exception. delete[] has no exceptions, so there is no need to handle exceptions in the destructor.

V
Vasya Pupkin, 2015-11-15
@EgorVoziyanov

template<class T>
ostream &operator<<(ostream &stream, const MainVector<T> &values) {
  stream << "[ ";
  copy(begin(values), end(values), ostream_iterator<T>(stream, " "));
  stream << ']';
  return stream;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question