Skip to content

Instantly share code, notes, and snippets.

@fbrosser
Created September 30, 2011 15:52
Show Gist options
  • Save fbrosser/1254188 to your computer and use it in GitHub Desktop.
Save fbrosser/1254188 to your computer and use it in GitHub Desktop.
C++ Hacking
#define _GLIBCXX_DEBUG
/*
* File: main.cpp
* Author: frebro
*
* Created on September 30, 2011, 5:26 PM
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#define tr(c, it) for(typeof(c.begin()) it = c.begin(); it != c.end() ; it++)
#define all(c) (c).begin(), (c).end()
using namespace std;
typedef vector<int> VI;
typedef set<int> SI;
bool comp (double i, double j) { return (i<j); }
int main(int argc, char** argv) {
int N;
double x;
cin >> N >> x;
vector<double> v (N);
vector<double>::iterator vit;
for (int i = 0; i < v.size(); i++) {
v[i] = -i/x;
}
sort (v.begin(), v.end(), comp);
sort (all(v), comp); //alternative
sort (all(v)); //standard comparator
cout << v.size() << endl;
cout << v.capacity() << endl;
for (vit=v.begin(); vit!=v.begin()+v.size(); ++vit) {
cout << " " << *vit;
}
tr (v, vit) { //alternative
cout << " " << *vit;
}
cout << endl << endl;
SI si1;
si1.insert(5);
si1.insert(3);
si1.insert(7);
tr(si1, it){
cout << *it << " ";
}
cout << endl << endl;
SI si2;
si2.insert(6);
si2.insert(5);
VI vi(100);
VI::iterator end = set_union(all(si1),all(si2), vi.begin());
vi = VI(vi.begin(), end);
tr(vi, it){
cout << *it << " ";
}
VI myvi(4);
// myvi[5]; //out of range crash
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment