home

Coding Question C++ for Python Users

24-03-2024

Variable

//python x = 1;
int x = 1;
// type name1 = value1;  name2 = value2; to save space;
ListNode *head=node1; *tail=node2;

For more information about initialisation, see this blog.

List

//python arr = [myfunc(x) for x in arr]
vector<T> array = std::transform(arr.begin(), arr.end(), target.begin(), myfunc);

//python max(arr)
std::max_element(begin(arr), end(arr))

Dictionary

// python: umap = {}
unordered_map<int, int> umap;

//python: if k in umap: ...
if (umap.count(key))

Macros

Using macros in C++ can shorten the code.

#define FORLOOP(start, end, increment) \
  for (int i = (start); i < (end); i += (increment))

FORLOOP(0, 10, 1) cout<<i<<endl;