Category Archives: C++

Another way to print hex value in C++

You can simply use the std::hex base format flag! This also works with oct and dec. #include <iostream> int main() { std::cout << "Value of 10 in hex: " << std::hex << 10 << std::endl; std::cout << "Value of 10 in dec: " << std::dec << 10 << std::endl; std::cout << "Value of 10 in […]

0  

Pass a STL Array of fixed size as a parameter to another function without declaring the size in the function signature or prototype

There are two ways of doing this. You can either pass in a pointer/reference to the array along with it’s size to the function. #include <iostream> #include <array> void swap(int *arr, int a, int b) { auto t = arr[a]; arr[a] = arr[b]; arr[b] = t; } int partition(int *arr, int low, int high) { […]

0  

Printing hex values using cout()

I wanted to display hex numbers and came across setf() & unsetf() functions which are quite useful to know of! There are different kinds of flags & masks you can use as well. Please refer http://www.cplusplus.com/reference/iostream/ios_base/setf/ for more information. #include <iostream> using namespace std; int main() { int num = 255; cout.setf(ios::hex, ios::basefield); cout << […]

0