C++ - Tips'n Tricks - Using a list of pairs

The container list may accept another container. For example a pair. So we are trying to add pairs inside a list. It will become a list of pairs! In the snippet below, I’m using only one file. So just compile and execute it to see the result. Let’s see this by creating a list of pairs in this tutorial. The file /* main.c */ #include #include #include class My { private: std::list < std::pair > _container; public: My() { std::cout << "My created." << std::endl; this->containerManager(); } virtual ~My() { std::cout << "My destroyed." << std::endl; } void containerManager() { std::cout << "Size of _container = " << this->_container.size() << std::endl; this->containerAddElement(std::pair("John", "Gray")); this->containerAddElement(std::pair("William", "Blue")); this->containerAddElement(std::pair("Charles", "Green")); std::cout << "Size of _container = " << this->_container.size() << std::endl << std::endl; this->containerDisplayElements(); this->containerUseFirstElement(); this->containerDisplayElements(); this->containerUseFirstElement(); this->containerDisplayElements(); } void containerAddElement(std::pair myPair) { std::cout << "Adding \"" << myPair.first << "\" and \"" << myPair.second << "\" in _container." << std::endl; this->_container.push_back(std::pair(myPair.first, myPair.second)); } void containerDisplayElements() { int k; std::list < std::pair >::iterator it; k = 0; for (it = this->_container.begin(); it != this->_container.end(); ++it) { std::cout << "*it " << k << " = " << (*it).first << " - " << (*it).second << std::endl; ++k; } std::cout << "First element in _container = " << this->_container.front().first << " - " << this->_container.front().second << std::endl << std::endl; } void containerUseFirstElement() { std::cout << "Pop!" << std::endl; this->_container.pop_front(); } }; int main() { My *my = new My(); delete my; while (1337); return 0; } Result My created. Size of _container = 0 Adding "John" and "Gray" in _container. Adding "William" and "Blue" in _container. Adding "Charles" and "Green" in _container. Size of _container = 3 *it 0 = John - Gray *it 1 = William - Blue *it 2 = Charles - Green First element in _container = John - Gray Pop! *it 0 = William - Blue *it 1 = Charles - Green First element in _container = William - Blue Pop! *it 0 = Charles - Green First element in _container = Charles - Green My destroyed. Well done! 🤩

April 11, 2012

C++ - Algorithm - Using the for_each() function

An example of how using the for_each() function. Notice that you can have the same result with an iterator, here an example. #include #include #include void myFunction(int myInt) { std::cout << "myInt = " << myInt << std::endl; } int main() { std::vector myVector; std::vector::iterator myIterator; int i; i = 0; while (i < 10) { myVector.push_back(i++); } for(myIterator = myVector.begin(); myIterator != myVector.end(); ++myIterator) { if (*myIterator == 2) { myVector.erase(myIterator); myVector.insert(myIterator, 80); } } std::for_each(myVector.begin(), myVector.end(), myFunction); return 0; } Result: ...

April 5, 2012

C++ - Standard library - Using the pair container

The pair container cannot be iterated. So we can use it for example to return a double value at the end of a function. In the snippet below we are going to use it to change the first and the second value. This, respectively with the pair.first and pair.second methods: #include #include int main() { std::pair myPair(1, "John"); std::cout << "Id: " << myPair.first << " - Name: " << myPair.second << std::endl; myPair.first = 38; myPair.second = "William"; std::cout << "Id: " << myPair.first << " - Name: " << myPair.second << std::endl; return 0; } Result: ...

April 5, 2012

C++ - STL (Standard Template Library) - Using the queue container

We are going to see in this queue tutorial, how to use it. Let’s see it with this first snippet. In the easy example below, we add 10 elements in the queue before displaying its content. We have to notice that we cannot iterate through a queue. So we have to use the couple queue.front() and queue.pop() to loop inside the queue. #include #include int main() { std::queue myQueue; int i; i = 0; while (i < 10) myQueue.push(i++); std::cout << "Size of the queue: " << myQueue.size() << std::endl; std::cout << "First element of the queue: " << myQueue.front() << std::endl; std::cout << "Last element of the queue: " << myQueue.back() << std::endl; while (!myQueue.empty()) { std::cout << myQueue.front() << std::endl; myQueue.pop(); } return 0; } Result: ...

April 5, 2012

C++ - STL (Standard Template Library) - Using the vector container

The vector container is the most classic STL container. In the snippet below, I can iterate through the vector, so I use an iterator to erase an element of the vector. In our case, I removed the number 2 from the container and I added 80 to this place. Let’s see this example of the vector container. Code #include #include int main() { std::vector myVector; std::vector::iterator myIterator; int i; i = 0; while (i < 10) { myVector.push_back(i++); } for(myIterator = myVector.begin(); myIterator != myVector.end(); ++myIterator) { if (*myIterator == 2) { myVector.erase(myIterator); myVector.insert(myIterator, 80); } } for(myIterator = myVector.begin(); myIterator != myVector.end(); ++myIterator) { std::cout << "*myIterator = " << *myIterator << std::endl; } return 0; } Result: *myIterator = 0 *myIterator = 1 *myIterator = 80 *myIterator = 3 *myIterator = 4 *myIterator = 5 *myIterator = 6 *myIterator = 7 *myIterator = 8 *myIterator = 9 A really helpful container! 😇

April 5, 2012

C++ - Tips'n Tricks - Converting a std:string into an int and converting an int to a std::string

For the C, we had the famous atoi() function, but for C++, how can I convert a std::string into an int? That’s a great question, and the answer is easy, with the std::stringstream type element. In this tutorial, we will see how to convert a std::string into an int and how to convert an int to a std::string. Let’s start by converting a string into a number (an int): 1. Converting a std::string into an int So, let’s take an example of this conversion: ...

February 10, 2012

C++ - Errors / Warnings - After compiling

As the same for the C - Error / Warning section, I made this one to summarize common mistakes when I tried to compile my code. So let’s go to see some good errors and warnings in C++ (I’m sure it is also a great moment for you when you discovered these errors). 1. Main.cpp:(.text+0x1f): undefined reference to `Parent::Parent()' A. The problem A very common error! Did you include the parent.cpp file in your Makefile? Did you compile with this parent.cpp? ...

January 15, 2012

C++ - Keyword - virtual

The virtual keyword is used in different ways in C++. The most common way to use it is before a destructor. Indeed, with this virtual keyword we can specified that the children of a class will be deleted before its parent. Let’s see it with a tutorial. 1. virtual before a destructor In this example, I created two classes, a Parent and a Child. So Child inherits from Parent. The headers: ...

January 15, 2012

C++ - Makefile - Adding flags

A personal Makefile is sometimes better than a Makefile generated by default by your IDE, such Eclipse or Visual Studio C++ for example. If you don’t know how to create one, let’s see it in this easy example of Makefile for C++. I added some flags for the variable CXXFLAGS, it is different from the C language, where it is CFLAGS. With g++, you do not have to write the variable CXXFLAGS in your compilation line, but if you prefer, you can add it. ...

January 15, 2012

C++ - Qt Framework - Hello World

Now that you have already installed Eclipse, Qt Framework Open Source and MinGW with the this tutorial, we will create our first program window. Let’s go. 1. Hello World on Windows OS Open Eclipse, on the Explorer > Right Click > New Project > Qt > Qt Gui Project. Write a project name such as hello-world, then Next, Next, Finish. A lot of files and directories are created for you. Delete all. ...

October 18, 2011