Tools and Environment Setup ********************************************* .. epigraph:: The purpose of a programming language is to help express ideas in code. -- Bjarne Stroustrup Operating System ================================= All the examples in this book works in `Ubuntu 20.04.4 LTS `_. .. code-block:: bash $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.4 LTS Release: 20.04 Codename: focal C++ Standard and Compiler ================================= In the past 10 years, C++ has drmatically changed. The C++ standard has evolved from C++98 to C++20 in order to embrace more modern programming language features. We will use C++20 as the standard for this book. However, in the interview, the focus is on data structure and algorithm. Unless there is a special requirement for candidates' C++ familiarity, C++98 is enough for the candidate to do the work. GNU g++, LLVM clang++, Microsoft Visual C++ are popular C++ compilers. In this book, clang++ 15.0.0 is used though it is still under development at the time of this writing [#]_. .. code-block:: bash $ clang++ --version Ubuntu clang version 15.0.0-++20220225053013+37e84d9be06d-1~exp1~20220225173121.170 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin To install clang 15, add the two lines in `/etc/apt/sources.list`: .. code-block:: bash deb http://apt.llvm.org/focal/ llvm-toolchain-focal main deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main Add gpg key to retrieve the archive signature: .. code-block:: bash wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add - Then use apt to update and install all necessary packages: .. code-block:: bash sudo apt update sudo apt install clang lld lldb clang-format clang-tidy -y Not all the language and library features in C++ standard are supported by compilers. .. rubric:: Footnotes .. [#] The latest updates are available at LLVM Debian/Ubuntu nightly packages (https://apt.llvm.org) C++ REPL ================================= C++ is a static language, in contrast with dynamic langues like Python. This makes it easy to write high performance program but sometimes hard to learn. Fortunately there is a REPL(read–eval–print loop) tool **cling** to help. A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise. **cling** is developed by CERN(European Organization for Nuclear Research) and hosted under its `ROOT project `_, where you can download Linux and Mac version. At the time of this writing, the latest version of **cling** is `cling_2020-11-05_ROOT-ubuntu2004.tar.bz2`. Run the following command to install **cling**: .. code-block:: bash wget https://root.cern.ch/download/cling/cling_2020-11-05_ROOT-ubuntu2004.tar.bz2 tar xjvf cling_2020-11-05_ROOT-ubuntu2004.tar.bz2 sudo mv cling_2020-11-05_ROOT-ubuntu2004 /usr/local export PATH=/usr/local/cling_2020-11-05_ROOT-ubuntu2004/bin:$PATH exec $SHELL Now you can run **cling** in the terminal and try some C++ code. .. code-block:: bash :linenos: $ cling -std=c++2a ****************** CLING ****************** * Type C++ code and press enter to run it * * Type .q to exit * ******************************************* [cling]$ #include [cling]$ #include [cling]$ #include [cling]$ #include [cling]$ namespace fs = std::filesystem; [cling]$ fs::current_path(fs::temp_directory_path()); [cling]$ fs::create_directories("sandbox/1/2/a"); [cling]$ fs::create_directory("sandbox/1/2/b"); [cling]$ fs::permissions("sandbox/1/2/b", fs::perms::others_all, fs::perm_options::remove); [cling]$ fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b"); [cling]$ std::system("ls -l sandbox/1/2"); total 12 drwxrwxr-x 2 henry henry 4096 Feb 27 10:19 a drwxrwx--- 2 henry henry 4096 Feb 27 10:19 b drwxrwx--- 2 henry henry 4096 Feb 27 10:19 c - Line 1 is to launch `cling` REPL environment. - Line 12 is to change current directory to system's temporary folder - Line 13, 14 and 16 are to create folders - Line 15 is to change permissions of one folder - Line 17 is to run a system command to print the contents of one folder The code snippet above shows the new standard does make the programmer life easier. With built-in filesystem library support in modern C++, we don't have to use third-party library to write cross-platform code. To save the typing, we can set alias: .. code-block:: bash alias cling='cling -std=c++2a' And put all the common C++ header files into one `sein.hpp`: .. code-block:: c++ #pragma once #include // Stream #include #include // STL #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Memory Management #include // Time #include // Assert #include // Exception #include // Multithread #include #include #include #include // Numerics #include // File System using namespace std::chrono; using namespace std; namespace fs = std::filesystem; In cling, we can just include `sein.hpp` and using the classes and functions directly. .. code-block:: c++ :linenos: :emphasize-lines: 2 $ cling [cling]$ #include "sein.hpp" [cling]$ vector v={"1","2"}; [cling]$ v.size() (unsigned long) 2 [cling]$ v (std::vector &) { "1", "2" } [cling]$ string s="hello__world"; [cling]$ size_t p= s.find("__"); [cling]$ p (unsigned long) 5 [cling]$ s.substr(0,5) (std::basic_string) "hello" [cling]$ s.substr(5+2) (std::basic_string) "world" .. note:: - use 'c++98' or 'c++03' for 'ISO C++ 1998 with amendments' standard - use 'gnu++98' or 'gnu++03' for 'ISO C++ 1998 with amendments and GNU extensions' standard - use 'c++11' for 'ISO C++ 2011 with amendments' standard - use 'gnu++11' for 'ISO C++ 2011 with amendments and GNU extensions' standard - use 'c++14' for 'ISO C++ 2014 with amendments' standard - use 'gnu++14' for 'ISO C++ 2014 with amendments and GNU extensions' standard - use 'c++17' for 'ISO C++ 2017 with amendments' standard - use 'gnu++17' for 'ISO C++ 2017 with amendments and GNU extensions' standard - use 'c++2a' for 'Working draft for ISO C++ 2020' standard - use 'gnu++2a' for 'Working draft for ISO C++ 2020 with GNU extensions' standard - use 'c++2b' for 'Working draft for ISO C++ 2023 DIS' standard - use 'gnu++2b' for 'Working draft for ISO C++ 2023 DIS with GNU extensions' standard Advice ================================= C++ is a hard language in terms of coding speed and convenience. As an interviewer, I normally give bonus points to the candidates if they can finish the same coding question with C++ while other candidates use other advanced languages like Python, Java or Go. My interview experience shows that hardcore programmers prefer C++ as their coding languages in the interview, and they normally have very good performance in the interview. It is not because C++ is a better language. It is just because it is hard, as there are not many handy helper functions available in C++. For example, splitting string is a simple task and can be done in Python with one line as below: .. code-block:: python :linenos: >>> s = "hello__world" >>> s.split('__') ['hello', 'world'] In C++, you have to write a function with many lines. The following is one of the possible implementations with eleven lines of code: .. code-block:: c++ :linenos: vector split(string s, const string& delimiter){ vector r; size_t pos = 0; while ((pos = s.find(delimiter)) != string::npos) { r.push_back(s.substr(0, pos)); s.erase(0, pos + delimiter.size());// or s=s.substr(pos+delimiter.size()) } if(!s.empty()) r.push_back(s); return r; } To finish the same task, the C++ candidate have to understand the usage of `string::find`, `string::substr`, `string::erase`, `string::npos` and `vector::push_back`. Moreover, C++ requires candidates to consider memory management, which is automatically taken care of by Garbage Collector in advanced languages. So if you want to stand out, be a hardcore programmer and get the bonus points, C++ is a good choice to show you skills and help you crack the data structure and algorithm problems in the competitive interview. Reference ================================= - Jupyter Xeus `Xeus: a C++ implementation of the Jupyter kernel protocol `_ - `Interactive C++ with Cling `_