Category: STL Algorithm
Using std::find & std::find_if with User Defined Classes
std::find and std::find_if algorithms are used to find first element in the given range. In this article we will see how to use std::find and std::find_if algorithm with user defined datatypes i.e. Classes & Structures. Advertisements Internally std::find algorithm uses the == operator for comparisons. For built-in data types like int, std::string etc operator == …
Using std::find & std::find_if with User Defined ClassesRead More
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
std::generate Tutorial and example
In this article we will see how to use std::generate and std::generate_n with STL sequential containers. std::generate is a STL algorithm useful in generating data for a sequential container by calling passed function object continuously. For example, Advertisements Suppose, we have a structure Task and a std::vector<Task * > and we want to initialize this …
STL Algorithm std::unique Tutorial
In this example we will discuss std::unique STL Algorithm. std::unique is a n STL Algorithms that provide a functionality to remove all consecutive duplicate elements from a given range. Advertisements For example, we have an array of elements i.e. 1,2,3,3,3,4,4,5,5 Frequently Asked: STL Algorithm std::unique Tutorial As we can see above there are 4 consecutive …
STL Algorithm – std::sort Tutorial and Example
In this article we will discuss STL Algorithm std::sort algorithm. std::sort is a STL Algorithm which provides the functionality of sorting a given range of elements. To use std::sort we need to pass start and end of range in it as an argument i.e. Advertisements std::sort (<START OF RANGE> , <END OF RANGE>); For example, we …
How to compare two vectors in C++
In this article, we will discuss different ways to compare two vectors in C++. Table Of Contents Comparing two vectors using operator == Comparing two vectors using STL Algorithm std::equal() Comparing two vectors with custom comparators Summary Best Courses to Learn Modern C++, like C++11, C++17 and C++20 Comparing two vectors using operator == std::vector …
C++ : How to get element by index in vector | at() vs operator []
In this article we will discuss different techniques to get an element from vector by index or position. In vector elements are indexed from 0 to size() – 1. To access any element in vector by index vector provides two member functions i.e. at() operator[] Let’s discuss them in detail, Advertisements Access an element in …
C++ : How to get element by index in vector | at() vs operator []Read More
C++ : How to find duplicates in a vector ?
In this article we will discuss how to find duplicate elements in vector and their repetition count. Suppose we have a vector of strings i.e. Advertisements // Vector of strings std::vector<std::string> vecOfStings{ “at” , “hello”, “hi”, “there”, “where”, “now”, “is”, “that” , “hi” , “where”, “at”, “no”, “yes”, “at”}; Let’s find duplicate elements from this …
Check if String is Palindrome in C++
This article will discuss different ways to check if a string is palindrome or not in C++. Table of Contents Check if a string is palindrome using STL Algorithm equal() Check if a string is palindrome using for loop Check if a string is palindrome using STL Algorithm reverse() There are different ways to do …
Check if a string contains certain characters in C++
In this article, we will discuss different ways to check if a string contains certain characters or not. Suppose we have a sequence of characters like, vector vecOfChars{‘s’, ‘a’, ‘i’}; We want to check if a given string contains all of these characters or not. Let’s see how to do this, Advertisements Check if a …
Check if a string contains certain characters in C++Read More