Skip to content

Instantly share code, notes, and snippets.

@Sixeight
Created June 10, 2009 16:56
Show Gist options
  • Save Sixeight/127346 to your computer and use it in GitHub Desktop.
Save Sixeight/127346 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
class Array : public vector<T> {
public:
Array() : vector<T>() {}
Array(int size) : vector<T>(size) {}
Array* sort()
{
std::sort(this->begin(), this->end());
return this;
}
Array* shuffle()
{
random_shuffle(this->begin(), this->end());
return this;
}
Array* uniq()
{
erase(unique(this->begin(), this->end()), this->end());
return this;
}
Array* push(const T& value)
{
push_back(value);
return this;
}
/* Not working around Array<T>::const_iterator
Array* inspect()
{
Array<T>::const_iterator it;
cout << '[';
for (it = this->begin(); it != this->end() - 1; it++) {
cout << *it << ", ";
}
cout << *it << ']' << endl;
return this;
} */
};
void show(const Array<int>* list)
{
Array<int>::const_iterator it = list->begin();
cout << '[';
for (; it != list->end() - 1; it++) {
cout << *it << ", ";
}
cout << *it << ']' << endl;
}
int main(int argc, char *argv)
{
Array<int>* list = new Array<int>;
for (int i = 0; i < 10; i++) {
list->push_back(i + 1);
}
show(list);
show(list->shuffle());
show(list->push(3)->push(5)->push(3)->sort()->uniq());
delete list;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment