Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created September 30, 2015 18:44
Show Gist options
  • Save krysseltillada/cdab94210611a4e8d50d to your computer and use it in GitHub Desktop.
Save krysseltillada/cdab94210611a4e8d50d to your computer and use it in GitHub Desktop.
Vec template
#include <iostream>
#include "Vec.hpp"
int main ()
{
Vec <std::string> vec1;
Vec <std::string> vec2 = vec1;
vec1.add ("sample");
if (vec1 == vec2)
std::cout << "true" << std::endl;
return 0;
}
#pragma once
#include <memory>
#include <vector>
template <typename> class Vec;
template <typename getType>
bool operator == (const Vec <getType> &, const Vec <getType> &);
template <typename getType>
bool operator != (const Vec <getType> &, const Vec <getType> &);
template <typename getType>
bool operator > (const Vec <getType> &, const Vec <getType> &);
template <typename getType>
bool operator < (const Vec <getType> &, const Vec <getType> &);
template <typename getType>
bool operator >= (const Vec <getType> &, const Vec <getType> &);
template <typename getType>
bool operator <= (const Vec <getType> &, const Vec <getType> &);
template <typename classType>
class Vec {
friend bool operator != <classType> (const Vec <classType> &, const Vec <classType> &);
friend bool operator > <classType> (const Vec <classType> &, const Vec <classType> &);
friend bool operator < <classType> (const Vec <classType> &, const Vec <classType> &);
friend bool operator >= <classType> (const Vec <classType> &, const Vec <classType> &);
friend bool operator <= <classType> (const Vec <classType> &, const Vec <classType> &);
friend bool operator == <classType> (const Vec <classType> &, const Vec <classType> &);
public:
typedef typename std::vector <classType>::iterator Iter;
typedef typename std::vector <classType>::const_iterator CIter;
Vec () :
data (std::make_shared <std::vector <classType> > ()) { }
Vec (std::initializer_list <classType> il) :
data (std::make_shared <std::vector <classType > > (il)) { }
Vec add (const classType &v) {
data->push_back (v);
return *this;
}
Iter Beg () {
return data->begin ();
}
Iter End () {
return data->end ();
}
CIter cBeg () const {
return data->begin ();
}
CIter cEnd () const {
return data->end ();
}
private:
std::shared_ptr <std::vector <classType> > data;
};
template <typename classType>
bool operator == (const Vec <classType> &lhs, const Vec <classType> &rhs) {
return *lhs.data == *rhs.data;
}
template <typename classType>
bool operator != (const Vec <classType> &lhs, const Vec <classType> &rhs) {
return *lhs.data != *rhs.data;
}
template <typename classType>
bool operator > (const Vec <classType> &lhs, const Vec <classType> &rhs) {
return *lhs.data > *rhs.data;
}
template <typename classType>
bool operator < (const Vec <classType> &lhs, const Vec <classType> &rhs) {
return *lhs.data < *rhs.data;
}
template <typename classType>
bool operator >= (const Vec <classType> &lhs, const Vec <classType> &rhs) {
return *lhs.data >= *rhs.data;
}
template <typename classType>
bool operator <= (const Vec <classType> &lhs, const Vec <classType> &rhs) {
return *lhs.data <= *rhs.data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment