std::vector of containers: better use pointers

Typical example: If you heavily use a vector of map, it’s more convenient to use a vector of pointers to maps. The java way.

If you drop the “pointers to”, then when you retrieve the map from the vector, you often get a copy, unless you do this:

map<int, int> * mapPtr = &v1[i];

By the way, here’s an initializer for std::map:

vec.push_back(map<int, int>{{32,1}} );

non-virtual dtor: some random scenarios

Background — How important are these scenarios? First off, tech quizzes are extremely important since you are judged just over a few questions. Second, these scenarios pop up by accidents, rather than be design, all the time in real projects. You better learn to deal with a drunken driver while on the road.

Better test these:

Q: what if Base and Derived dtor both non-virtual and an autoVar is destroyed?
%%A: see post on DCBC.

Q: What if Base dtor is not virtual but Derived is virtual, and a Derived auto variable is destroyed on the stack?
%%A: For an autoVariable that’s not deleted via a ptr, Derived ctor (virtual or not) runs, followed by Base dtor. Same DCBC

Note the well-known __undefinedBehavior__ affects delete only, not stack variables or static variables.

Note virtual keyword affects pointer variable. Non-ref variables aren’t affected.

(intuitive)derivation of the combination formula

Q1: how many ways to pick 3 boys out of 7 to form a choir?

Suppose we don’t know the 7_choose_3 formula, but my sister said answer is 18. Let’s verify it.

How many ways to line up the 7 boys? 7!

Now suppose the 3 boys are already picked, and we put them in the front 3 positions of the line.

Q2: Under this constraint, how many ways to line up the 7 boys?
A2: In the front segment, there are 3! ways to line up the 3 boys; in the back segment, there are 4! ways to line up the remaining 4 boys. So answer is 3! x (7-3)! = 144

Since there are supposedly 18 ways to pick, then 18 * 144 must equal 7! We find out 18 is wrong answer.

all permutations of Any combination of N boys

A standard permutation/combination problem. In coding tests, you are often required to iterate all of them.

Given N cities, how many permutations of any combinations are there in total.

My iterative sum formula: answer(N)= N_choose_0 + N_choose_1 * 1! + N_choose_2 * 2! + … + N_choose_N * N!

N_choose_0 = 1 !

It is tempting to use answer(N-1) and build a recursive algorithm, but the iterative formula is superior.

c++ print stacktrace

Not needed in any interview…

Here’s a simple demo generating a back trace in a c++ source code. Not sure if it would work in a big code base.

#ifdef usage_demo

g++ -g -rdynamic backtrace.cpp # -rdynamic needed
./a.out | perl -pe 's/.*\((.*)\+.*\[(.*)\]/ `c++filt $1` . `addr2line $2` /e;'

#endif

#include <execinfo.h>
#include <stdexcept>
using namespace std;
int f2(){
        try{
                throw 1;
        }catch(...){
                void *array[10];
                size_t size = backtrace(array, 10);
                backtrace_symbols_fd(array, size, STDOUT_FILENO);
        }
}
int f1(){
        return f2();
}
int main(){
        f1();
}