How do I declare a 2d array in C++ using new?

If your row length is a compile time constant, C++11 allows See this answer. Compilers like gcc that allow variable-length arrays as an extension to C++ can use new as shown here to get fully runtime-variable array dimension functionality like C99 allows, but portable ISO C++ is limited to only the first dimension being variable. Another efficient option is … Read more

How to make a flat list out of a list of lists

Given a list of lists t, which means: is faster than the shortcuts posted so far. (t is the list to flatten.) Here is the corresponding function: As evidence, you can use the timeit module in the standard library: Explanation: the shortcuts based on + (including the implied use in sum) are, of necessity, O(T**2) when there are T sublists — as the intermediate … Read more