Skip to content

Instantly share code, notes, and snippets.

@palcu
Created April 10, 2016 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palcu/01f8cd2244691c68f941aaaa6fe070b7 to your computer and use it in GitHub Desktop.
Save palcu/01f8cd2244691c68f941aaaa6fe070b7 to your computer and use it in GitHub Desktop.
Advice C++ InfoClock
// Includes all headers
#include <bits/stdc++.h>
// Don't be afraid to use constants
const int N = 1024;
void do_not_use_endl() {
vector<int> primes {2, 3, 5, 8, 11};
// Let's tail -f the output file
for (int i=0; i<primes.size(); i++) { // Use variables as local as possible
cout << primes[i] << endl; // This flushes the output everytime
sleep(1);
}
cout << "Now with newline\n";
for (int x : primes) { // Nice syntax for iterating
cout << x << "\n";
sleep(1);
}
}
void sexy_pairs() {
cout << "=== Pairs\n";
string day = "Monday";
// equivalent to
string same_day {"Monday"};
pair<int, string> my_pair {1, day};
cout << my_pair.first << " " << my_pair.second << "\n";
// Containers
vector<pair<int, string>> weekdays;
weekdays.push_back(my_pair);
weekdays.push_back({2, "Tuesday"});
cout << "=== Range loops";
for (auto p : weekdays) {
cout << p.first << " - " << p.second << "\n";
}
}
void lambdas() {
vector<pair<int, int>> v;
for (int i=0; i<10; i++) {
v.push_back({rand() % 10, rand() % 20});
}
sort(v.begin(), v.end(), [] (pair<int,int> a, pair<int,int> b) {
return a.second > b.second;
});
for (auto p : v) {
cout << p.first << " - " << p.second << "\n";
}
}
void hashes() {
// O(1) access/update time... but it's unordered
unordered_map<string, int> hash;
hash["python"] = 42;
hash["infoclock"] = 333;
hash["wine"] = 70;
for (auto p : hash) {
cout << p.first << " - " << p.second << "\n";
}
}
int main() {
// This isn't executed on Codeforces/Infoarena.
#ifndef ONLINE_JUDGE
freopen("cdf.out", "w", stdout);
#endif
// Read this before http://algopedia.ro/wiki/index.php/Note_de_curs,_clasele_11-12,_21_noiembrie_2013
srand(time(NULL));
// Constructor for an array of 100 zeros.
vector<int> v(N, 0);
do_not_use_endl();
sexy_pairs();
lambdas();
hashes();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment