How to use bitmask?

Bit masking is “useful” to use when you want to store (and subsequently extract) different data within a single data value. An example application I’ve used before is imagine you were storing colour RGB values in a 16 bit value. So something that looks like this: You could then use bit masking to retrieve the …

Read more

c++ vector bubble sort

You need to pass by reference; by making a copy, you sort a temporary copy, and then that’s it; it disappears and the original is still unsorted, because, once again, you sorted only a temporary copy of the entire vector. So change vector<int> a to vector<int>& a. Here’s the code, fixed:

Does static constexpr variable inside a function make sense?

The short answer is that not only is static useful, it is pretty well always going to be desired. First, note that static and constexpr are completely independent of each other. static defines the object’s lifetime during execution; constexpr specifies that the object should be available during compilation. Compilation and execution are disjoint and discontiguous, both in time and space. So once the program is …

Read more

Compiling C++11 with g++

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable. Assuming you are invoking g++ from the command line (terminal): $ g++ -std=c++11 your_file.cpp -o your_program or $ g++ -std=c++0x your_file.cpp -o your_program if the above doesn’t work.

Creating folders in C++

I have recently started working in C++ and came across this situation when I have to create a directory while executing my code. The code is working fine when I have to create a single folder but it fails when I have to create another folder withing this newly created folder. Suppose, I am in …

Read more

too many initializers for ‘int [0]’ c++

In C++11, in-class member initializers are allowed, but basically act the same as initializing in a member initialization list. Therefore, the size of the array must be explicitly stated. Stroustrup has a short explanation on his website here. The error message means that you are providing too many items for an array of length 0, …

Read more