The Standard Template Library (STL) is a software library originally designed by Alexander Stepanov for the C++ programming language [...]. It provides four components called algorithms, containers, functions, and iterators. [wikipedia]
We do not go into details here, so just take a look at our examples
Converting an expression of a given type into another type is known as type-casting. [cplusplus]
Implicit conversions do not require any operator. They are automatically performed when a value is copied to a compatible type.
int8_t int_short = 127;
int32_t int_long = int_short; // implicit conversion
C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion.
int8_t int_short = 127;
int32_t int_long;
int_long = (int32_t)int_short; // c-like cast notation
int_long = int32_t(int_short); // functional notation
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.
Therefore, dynamic_cast is always successful when we cast a class to one of its base classes:
class CBase
{
};
class CDerived : public CBase
{
};
CBase base;
CBase* pbase;
CDerived derived;
CDerived* pderived;
pbase = dynamic_cast<CBase*>(&derived); // ok: derived-to-base
// pderived = dynamic_cast<CDerived*>(&base); // wrong: base-to-derived
static_cast can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived. [...] static_cast can also be used to perform any other non-pointer conversion that could also be performed implicitly.
double dpi=3.14159265;
int ipi = static_cast<int>(dpi);
class CBase
{
};
class CDerived : public CBase
{
};
CBase* base = new CBase;
auto derived = static_cast<CDerived*>(base);
Re-use the result of the Animal-Cat-Bird exercise. Extend this exercise with the following functionality:
You can find the code for this exercise in the file stl_and_casting_exercise.cpp
An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero (tutorialspoint)
Always differentiate between Errors (assert) and Exceptions
Take a look at this really good article!
Use asserts to check for errors that should never occur. Use exceptions to check for errors that might occur [...]. (microsoft)
Throwing an exception is quite easy:
throw std::logic_error("Test throwing an exception");
// compare to assertion
assert(my_variable==5);
To get a nice overview about exceptions, take a look at tutorialspoint
auto division(const int par_a, const int par_b) -> double {
if (par_b == 0) {
throw std::invalid_argument("Division by zero!");
}
return (static_cast<double>(par_a) / static_cast<double>(par_b));
}
auto main() -> int {
int var_x = 50;
int var_y = 0;
double var_z = 0.0;
try {
var_z = division(var_x, var_y);
std::cout << var_z << std::endl;
}
catch (std::invalid_argument& invalid_exception) {
// in case of an "invalid_exception", we'll end up here
std::cerr << invalid_exception.what() << std::endl;
}
return 0;
}
Implement your own exception and write a small application to throw and catch this exception.
const char* what() const
method to return your own exception message.You can find the code for this exercise in the file exceptions.cpp
Generate documentation from source code. Doxygen is the de facto standard tool for generating documentation from annotated C++ sources [doxygen]
Check out the example from our learn2code 1 demo project
Install doxygen to your codespace by editing your Dockerfile:
sudo apt-get install -y doxygen graphviz
(Check this link for installation on Mac brew install doxygen
)
Running doxygen is quite easy. Go to the location of your Doxyfile file and run:
doxygen Doxyfile
If you've done everything right, you should be able to open the file html/index.html
with your browser .
To allow doxygen to collect all the documentation of your code, you have to use doxygen-style commenting. You can use this for all kinds of code like: classes, functions, variables, and many more.
/**
* @brief add a book to the library database
*
* @param lib library
* @param title title of the book
* @return book const* pointer to the added book, NULL if invalid
*/
book const *library_add_book(library *lib, const char title[]);
If you want to automatically generate the documentation and publish it to your GitHub Pages you have to enable them:
|
Read the docs
Install OpenCV to your codespace by editing your Dockerfile:
sudo apt-get install -y libopencv-dev
(Check this link for installation on Mac brew install opencv
)
# search for module opencv
find_package( OpenCV ) # alternative: find_package( OpenCV REQUIRED )
# continue only if opencv is found
if(OpenCV_FOUND)
# add executable for opencv hello world
add_executable(opencv_hello src/hello_world.cpp)
# include and link all opencv stuff
target_include_directories(opencv_hello PUBLIC ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( opencv_hello ${OpenCV_LIBRARIES})
endif()
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
auto main() -> int
{
// initialize a 120X350 matrix with 3 channels of black pixels:
cv::Mat output = cv::Mat::zeros(120, 350, CV_8UC3);
// write text on the matrix:
cv::putText(output, "Hello World :)", cv::Point(15, 70), cv::FONT_HERSHEY_PLAIN, 3, cv::Scalar(0, 255, 0), 4);
// write the resulting image to file
cv::imwrite(std::string(OUTPATH) + "/opencv_hello.jpg", output);
return 0;
}
Where is OUTPATH coming from
It is a Compiler Definition from CMake
target_compile_definitions(opencv_hello PRIVATE OUTPATH="${CMAKE_CURRENT_BINARY_DIR}")
Write a small opencv application to calculate the canny edges
You can find the code for this exercise in the file canny_edges.cpp
If you want to get closed contours from a binary or edge image corresponding to the official tutorial, you can take a look at the contours example
Using the CvPlot library, create a simple plot like:
The documentation and the tutorial will help you.
You can find the code for this exercise in the file hello_world.cpp.
Using the Rapidcsv library to read in this csv (Comma Separated Values) file.
The documentation and the examples will help you.
You can find the code for this exercise in the file read_csv.cpp.
The National Oceanic And Atmospheric Administration (NOAA) provides extensive weather statistics from all over the world.
We want to use the Global Summary of the Year dataset for the weather station in Konstanz.
Using the dataset, we want to create a diagram for the average, the maximum and the minimum temperature of the year in Konstanz as shown on the next slide.
You can find the code for this exercise in the file weather_analysis.cpp
The nature of a measurement is it's uncertainty. We want to read in a point a list of measurements from a csv file and calculate some statistics for it. We are interested in the mean, the variance, and the covariances. Finally we want to visualize the point cloud and it's uncertainty ellipse.
How such a visualization looks like is depicted in the following diagram.
You can find the code for this exercise in the file uncertainty_ellipse.cpp