Definition: A reference variable is a “reference” to another variable. Better definition: A reference variable is just another pointer variable which will take its own space in memory from here References are nothing but constant pointers in C++. A statement int &i = j; will be converted by the compiler to int *const i = &j . Again from here Usage: Declare a reference as such with the
Pointers in C++: The newbies guide
Pre-requisite You know pointers are important so you’re reading this How is data stored? When you declare variables, they are assigned a location in the computer’s memory. This location/address is actually where the value of the variable is stored. Let’s see a small example. You might not understand the code right, now that’s ok. Just pay attention to the output of cout and how it
Header files
“Header files, or .h files, allow related function, method, and class declarations to be collected in one place.”. This is useful as you can group together specific functionality and then export and use it at another place/file to be used by another program. There are 2 file extensions that we deal with “.h” and “.cpp”. The header file will have a “.h” extension whereas, the corresponding functions,
A* Search Algorithm in C++
In short the A* search algorithm lets you find the quickest possible path between 2 nodes. The process of finding a path between the start point and end point is called “Planning”. For robots this is called “Robot Motion Planning”
Enumerated types (Enum) in C++
Enum is a user defined data type in C++ whose value is equal to some user defined values. For example: Enum is useful for switch cases, when we expect a variable to have only one of the possible set of values. For example, we can change the code to:
Adding/pushing data to a vector in C++
Adding data to a vector in C++ rephrased means “Push data/element into an array” in JavaScript. JavaScript uses the “push” method whereas C++ uses “push_back” In JavaScript you push an element into an array like so: The same way in C++ :
File Streams in C++
This section could have been better. While not terrible, it misses alot in the hopes of not being too lengthy or verbose (my guess). So far cout has been used in order to print things referenced by variables, ints or strings to the console. The “cout” functionality has been provided by the iostream standard library. Files can also be read by what is known as
Processing strings in C++ with
<sstream> let us store streamed data instead of just outputting it to cout. So we can read whole files. Input Stream with istringstream object: “Once an istringstream object has been created, parts of the string can be streamed and stored using the “extraction operator”: >>. The extraction operator will read until whitespace is reached or until the stream fail” Streaming int The istringstream can stream
If-while statements in C++
If and while statements are pretty powerful as you can control when statement/statements of code are executed. Syntax: Example: While While statements allow you to execute a block of code till/while a condition is true Syntax: Example: Print out numbers 0-5 while i is less than or equal to <= 5
Printing out a 2D vector in C++
Here’s a nice pic of a 2D vector incase you’re wondering what it is to begin with: Declare, initialize a 2d vector and print it out Output: