rewriting cJoin in C++