Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created July 20, 2015 14:03
Show Gist options
  • Save krysseltillada/4713f7e8e2f2b902d933 to your computer and use it in GitHub Desktop.
Save krysseltillada/4713f7e8e2f2b902d933 to your computer and use it in GitHub Desktop.
data structure (own implementation)
#include <iostream>
#include <vector>
#include <algorithm>
class Records { /// interface
public:
std::string &vec_ret (unsigned);
const std::string &vec_ret (unsigned) const;
const Records &push (const std::string &) const;
Records &push (const std::string &);
Records &Sort_asc ();
Records &Sort_dsc ();
const Records &Sort_asc () const;
const Records &Sort_dsc () const;
unsigned Size ();
const unsigned Size () const;
explicit Records () = default;
explicit Records (unsigned sz) :
vec_rec (sz) { };
private:
mutable std::vector <std::string> vec_rec;
};
/// implementation
unsigned Records::Size ()
{
return vec_rec.size();
}
const unsigned Records::Size () const
{
return vec_rec.size();
}
std::string &Records::vec_ret (unsigned ind) {
return this->vec_rec[ind];
}
const std::string &Records::vec_ret (unsigned ind) const {
return this->vec_rec[ind];
}
const Records &Records::push (const std::string &str) const
{
this->vec_rec.push_back(str);
return *this;
}
Records &Records::push (const std::string &str)
{
this->vec_rec.push_back(str);
return *this;
}
Records &Records::Sort_dsc ()
{
std::sort (vec_rec.begin(), vec_rec.end (), std::greater <std::string> ());
return *this;
}
Records &Records::Sort_asc ()
{
std::sort (vec_rec.begin(), vec_rec.end(), std::less <std::string> ());
return *this;
}
const Records &Records::Sort_dsc () const
{
std::sort (vec_rec.begin(), vec_rec.end (), std::greater <std::string> ());
return *this;
}
const Records &Records::Sort_asc () const
{
std::sort (vec_rec.begin(), vec_rec.end(), std::less <std::string> ());
return *this;
}
int main ()
{
const Records people;
people.push ("kryssel tillada");
people.push ("miko tillada");
people.push ("gina tillada");
people.push ("gerrard t de leon");
people.push ("kanade mitsumi");
people.Sort_asc();
for (unsigned i = 0; i != people.Size(); ++i )
std::cout << people.vec_ret(i) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment