Skip to content

Instantly share code, notes, and snippets.

@icyflame
Last active October 27, 2017 12:04
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 icyflame/d925cb1111a29d2885d31ad633d56056 to your computer and use it in GitHub Desktop.
Save icyflame/d925cb1111a29d2885d31ad633d56056 to your computer and use it in GitHub Desktop.
A cheat sheet for C++

SORT

include algorithm, vector in C++11

int *arr = NULL;
arr = (int *) malloc(sizeof(int) * len);
std::vector<int> arrv(arr, arr+len);
int temp; cin >> temp;
arrv.push_back(temp); // add an element to the array
std::sort(arrv.begin(), arrv.end()); // MERGE SORT
arrv.clear(); // empty the array

PAIR

include vectory, utility

vector<pair<int, int>> arrv;
arrv.push_back(make_pair(7, 8));
for (const auto &n : arrv) { cout << n.first << ", " << n.second; }

HASHMAP

include unordered_map, string in C++11

char *key = "TEST";
int value = 10;
unordered_map<string, int> hashmap = { };
hashmap.insert({ key, value });
hashmap.insert({ "test", 55 });
cout << hashmap["test"];
cout << hashmap[key];
for (const auto &n : hashmap) {
    cout << n.first << "=" << n.second;
}
cout << hashmap["key not there"] << " must be 0" << endl;
unordered_map<string, int> copy_map = { };
copy_map.insert(hashmap.begin(), hashmap.end());
hashmap["test"] += 5;

Notes

  • max heap => root is the max element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment