de en es fr nl pl pt sv zh

cxx

Trace Source Code with Vim and Ctags

Logan

Ctags is a source code indexing tool. With ctags, we can easily find the definitions of the classes, functions, and variables. According to my experiences, ctags can significantly reduce the time to browse the source code. In this post, I would like to give a brief introduction to ctags and...

C++11 Unique Pointer

Logan

C++11 introduced three new smart pointer class templates: std::unique_ptr, std::shared_ptr, and std::weak_ptr. These smart pointer class templates are designed to replace the old std::auto_ptr smart pointer, which is known to have some defect and deprecated now. In this post, I would like to give...

C++ std::multimap and Equal Range

Logan

Today I have encountered a problem: Given that there are multiple equivalent keys in an instance of std::multimap, how could we list all of the corresponding values? For example: #include <map> #include <iostream> int main() { std::multimap<int, int> xs;...

C++ Virtual Destructor and Inheritence

Logan

It is a well-known idiom to define a virtual destructor for the classes with virtual functions. If we don't define a virtual destructor, then the base class destructor will be invoked when you are deleting the object through the base class pointer even if the object is an instance of derived...

C++ Private Inheritence and Using Directive

Logan

I used to feel that the private inheritence is useless. Although we can implement the has-a semantics with private inheritence, it provides little benefits compared with object composition. Besides, in order to expose the privately inherited members to public, C++ introduced an awkward syntax,...

C++ std::list Operations

Logan

To sort the doubly linked list std::list, we can simply call the sort() member function. For example, #include <iostream> #include <list> int main() { std::list<int> xs{5, 4, 3, 2, 1}; xs.sort(); // Sort the std::list! for (auto &x : xs) { std::cout << x...

C++ Associative Container and Iterator Validness

Logan

I used to believe that iterators will be invalidated after calling the member functions insert() or erase() of containers. Thus, I would adopt a conservative approach: Create a temporary container. Copy the elements which I would like to keep to the temporary container. Swap the container. For...