Tag | cxx

  1. Trace Source Code with Vim and Ctags

    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 …

    More

  2. C++11 Unique Pointer

    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 a …

    More

  3. C++ std::multimap and Equal Range

    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;
      xs.insert(std::make_pair(1, 12));
      xs.insert(std …

    More

  4. C++ Virtual Destructor and Inheritence

    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 …

    More

  5. C++ Private Inheritence and Using Directive

    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, i.e. the using directives. These …

    More

  6. C++ std::list Operations

    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 << std::endl;
      }
    }
    

    We can't …

    More

  7. C++ Associative Container and Iterator Validness

    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:

    1. Create a temporary container.
    2. Copy the elements which I would like to keep to the temporary container.
    3. Swap the container.

    For example, to remove …

    More