std::vector
// constructors
std::vector<int> v1;
std::vector<int> v2(5); // size of 5
std::vector<int> v3(5,1); // 5 ints with value 1
std::vector<int> v4(v3.begin(), v3.end()); // copying v3
std::vector<int> v5(v3); // copying v3
// construct from arrays
int int_arr[] = {2, 5, 7};
std::vector<int> v6(int_arr, int_arr + sizeof(int_arr) / sizeof(int));
v6[2] = 3;
std::cout << v6[2];
v6.size();
v6.push_back(3);
v6.emplace_back(...);
v6.back();
v6.pop_back();
v6.clear();
v6.erase(iter); // return the iterator of the next element.
v6.insert(iter, 5); // insert 5 before the iter element.
sort(v6.begin(), v6.end());
for (auto iter = v6.begin(); iter != v6.end(); iter++) {}
std::unordered_map
std::unordered_map<int, int> map;
map[1] = 100;
auto found = map.find(2);
if (found != map.end()) {}
std::cout << found->first << found->second; // key, value
std::heap
std::string
std::queue
std::stack
arrays
string persons[5];
string cars[3] = {"Volvo", "BMW", "Ford"};
string functions
// string to integer
const std::string str = "-12.3";
const int i = std::stoi(str); // -12
// string to long
const int i2 = std::stol(str); // -12
new data
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* createNewRoot(TreeNode* root, int val) {
TreeNode *new_node = new TreeNode(val, root, nullptr);
return new_node;
}
};