Tag Archives: pointer

C++ || Snippet – Custom “Shuffle” Function For Arrays

The following is a custom “Shuffle” function which shuffles the contents of an array. This function rearranges each item within the array by swapping the value of a given item with that of some other randomly selected item.

The code demonstrated on this page is different from the STL random_shuffle function in that this implementation is meant to work mainly on standard arrays.

This implementation revolves around using a series of void pointers.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

C++ || Snippet – Custom “For Each” Iterator Function For Arrays

A “For Each” algorithm is a function that iterates over all the elements contained in a storage collection, and calls a separate user defined function to manipulate each element within that collection.

The code demonstrated on this page is different from the STL for_each function in that this implementation is meant to work mainly on standard arrays.

The example below illustrates the “For Each” concept in a fully compilable and working demonstration. This example is trivial in that all it does is call a separate user defined function to display all of the contents within each array. This implementation revolves around using a simple function pointer and a series of void pointers.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output