Tag: Element
Remove all occurences of an element from vector in O(n) complexity
Suppose we have a vector of integers and we want to delete all occurences of a number from it i.e. Let’s say vector contain following numbers 1,2,5,4,5,1,5,7,8,9. Now we want to delete all the occurences of 5 from it, so that vector contents should become – 1 2 4 1 7 8 9 . Also …
Remove all occurences of an element from vector in O(n) complexityRead More
How to Search for an element in HashSet in Java
In this article we will discuss how to search for an element in HashSet. Main feature of a HashSet is Fast searching. Which it achieves using Hashing. Now suppose we want to search if an element exists in HashSet ? Advertisements It can be done using contains() member function of HashSet i.e. // It checks if the given …
How to add an element in Vector using vector::push_back
In this article we will discuss how to add element in a vector. std::vector provides a member function to push an element at the end i.e. Advertisements void push_back (const value_type& val); It will push the element in the end of list. As, this function adds the element, so it also increases the size of …
How to add an element in Vector using vector::push_backRead More
How to Search an element in unordered_set
In this article we will see how to search if an element is present in an unordered_set or not. std::unordered_set provides a member function find () that accepts an element of same type as set and search for that element in set. Advertisements If element is found then it returns the iterator pointing to that …
How to find an element in unordered_map
In this article we will discuss how to find an element in an unordered_map. An unordered_map store elements in key – value pair. Hence to search an element we need to search if the key exists in an unordered_map or not. For this unordered_map provides a member function find() i.e. Advertisements iterator find ( const …
How to Erase / Remove an element from an unordered_map
In this article we will discuss the different ways to remove an element from an unordered_map. Unordered_map provides different overloaded version of erase() member function to delete key value pairs from map. Advertisements Erasing an element from an unordered_map by key Unordered_map provides following overloaded version of erase() that accepts a key by value and …
How to Erase / Remove an element from an unordered_mapRead More
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
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. …
C++ List – Find | Contains : How to search an element in std::list ?
In this article we will discuss different ways to find or search a given element in the list. std::list does not provide ant find() or contains() method. So, if we want to search for an element in list or check if an element exists in std::list, then we not to write some code for it i.e. …
C++ List – Find | Contains : How to search an element in std::list ?Read More