Tag: by
Learn State Design Pattern by Designing a Simple Music System
Many times we need to develop systems / objects that provides services to outside world. But the response of each service is not always same, it depends upon its current internal state. When its internal state changes at run-time then its behaviour changes and same services acts differently. Advertisements For example, Suppose our system is …
Learn State Design Pattern by Designing a Simple Music SystemRead More
How to Sort a Map by Value in C++
In this article we will discuss how to sort a map by value in both ascending and descending order. A Map store the elements in the sorted order of keys. Advertisements For example, we have a map of words and its frequency count as key – value pair i.e. std::map<std::string, int> mapOfWordCount ; Now lets insert following …
How to search by value in a Map | C++
In this article will discuss how to search for all the elements in map with given value. Map internally store elements in Key-Value pair. In which keys are unique but values can be duplicate. There are member functions to search pairs by key i.e. std::map::find(). But there is no direct function to search for all …
C++ map : Erase element by key or Iterator or Range
In this article we will discuss the different ways to delete a key-value pair from map. std::map provides 3 overloaded version of erase() to remove elements from map i.e. Advertisements Erase by key Erase by Iterator Erase a range Lets discuss them one by one, Erase Element from Map by Key std::map provides a erase function that accepts …
C++ map : Erase element by key or Iterator or RangeRead More
C++ Map: Erase by Value or Callback while iterating | erase_if for map
In this article we will discuss two different ways to erase elements from map i.e. In previous article we discussed how to erase elements by key and iterator using member function std::map::erase. But there is no default function to delete elements by value instead of key or delete elements based on callback. Advertisements We will …
C++ Map: Erase by Value or Callback while iterating | erase_if for mapRead More
C++11 : Start thread by member function with arguments
In this article we will discuss how to start a thread by a member function of class. Starting thread with non static member function Suppose we have a class Task, which has non static member function execute() i.e. Advertisements class Task { public: void execute(std::string command); }; Now we want to start a thread which …
C++11 : Start thread by member function with argumentsRead More
How to Access Element by index in a Set | C++
In this article we will discuss how to access an element by index in a Set. std::set is an associative container, which internally store elements in a balanced binary search tree and it doesn’t provide random access operator []. Advertisements Therefore accessing a random element from set by index is little tricky. Accessing nth element in …
C++ : How to get element by index in List
In this article we will discuss how to get element by index in std::list. std::list doesn’t have operator [] std::list does not have random access operator [] because std::list internally store elements in doubly linked list. So, to access an element at nth location we need to iterate one by one from beginning to nth element. …
Java : Creating HashMap by associating multiple values with same Key
In this article we will discuss how to create a HashMap with multiple values associated with same Key in Java. Suppose we want to create a HashMap to keep the track of strings and their occurrences in text. For example. Advertisements “the” occurred at following indexes 1,4,8,12. “at” occurred at following indexes 22, 28, 44 …
Java : Creating HashMap by associating multiple values with same KeyRead More
C++ : How to read a file line by line into a vector ?
In this article we will discuss how to read a file line by line and put them in a vector or perform some other operation each line. Reading File line by line First open the file i.e. Advertisements // Open the File std::ifstream in(“file.txt”); Now keep reading next line using getline() and push it in …
C++ : How to read a file line by line into a vector ?Read More