Found 7346 Articles for C++

C++ Program to Implement Map in STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

3K+ Views

A Map is an associative container that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.Functions are used here:m::find() – Returns an iterator to the element with key value ‘b’ in the map if found, else returns the iterator to end.m::erase() – Removes the key value from the map.m:: equal_range() – Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to key.m insert() – To insert ... Read More

C++ Program to Implement List in STL

Nitya Raut
Updated on 30-Jul-2019 22:30:25

241 Views

A list is a sequence container that allow non-contiguous memory allocation. List has slow traversal as compared to vector, but once a position has been found, insertion and deletion are quick.Functions and descriptions:From main(), we have called following functions:    fl.resize() = Returns the resize of list.    fl.push_front() = It is used to push elements into a list from the front.    fl.remove() = Deletes elements from list.    fl.unique() = Deletes duplicate elements from list.    fl.reverse() = Reverses the list.    fl.front() = Returns the front element of list.Example Code#include #include #include #include using ... Read More

C++ Program to Implement Forward_List in STL

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

168 Views

Forward list in STL implements singly linked list. List is different by forward_list that list keeps track to both next and previous elements.While forward list keeps track of location of only next elements, thus increasing the storage space required to store each element. The disadvantage of forward_list is that is individual elements cannot be accessed directly and it cannot be iterated backwards.Functions and descriptions:From main(), we have called following functions:    fl.resize() = Returns the resize of forward_list.    fl.push_front() = It is used to push elements into a foward_list from the front.    fl.remove() = Deletes elements from forward_list. ... Read More

C++ Program to Implement Deque in STL

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

334 Views

Double Ended Queue is a Queue data structure in which the insertion and deletion operations are performed at both the ends (front and rear). Data can be inserted at both front and rear positions and can be deleted from both front and rear positions.AlgorithmBegin Declare deque vector and iterator. Take the input as per choice. Call the functions within switch operation: d.size() = Returns the size of queue. d.push_back() = It is used to push elements into a deque from the back. ... Read More

Binary Search functions in C++ STL

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

819 Views

Binary search is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the sorted array. The time complexity of binary search is O(1). This is a C++ program in which we implement various. Binary Search functions in C++ STLAlgorithmBegin    Initialize the vector of integer values.    The functions are used here:    binary_search(start_pointer, end_pointer, value) = Returns true if the value present in array otherwise    false.    lower_bound(start_pointer, end_pointer, value) = Returns pointer to “position of value” if container ... Read More

4 Dimensional Array in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

690 Views

A 4 dimensional array is an array of 3Darrays.AlgorithmBegin. Declare the variables. Declare the array elements. Take the no of elements as input. Take the elements as input. Print the elements stored in array. End.Here is an example of 4D array.#include using namespace std; int main() {    int a[2][2][3][2];    cout > a[i][j][k][l];             }          }       }    }    cout

Why C/C++ array index starts from zero?

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

603 Views

As Array index starts with 0, so a[i] can be implemented as *(a + i).If Array index starts with 1 then a[i] will be implemented as *(a+i-1) which will be time consuming during compilation and the performance of the program will also be effected.So, it is better to start index of the array from 0.A simple program of array is given -Example Codeint main() {    int array[5] = {7, 7, 7, 6, 6};    for (int i = 0; i < 5; i++)       cout

An Uncommon representation of array elements in C/C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

79 Views

This is a simple C++ program of an uncommon representation of array elements.#include using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout

Short hand array notation in C/C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

338 Views

If there are repeated values are present in C then we use shorthand array notation to define that array.Here is an example:Example Code#include int main() { int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}; for (int i = 0; i < 10; i++) printf("%d ", array[i]); return 0; }Output7 7 7 7 6 6 2 2 2 2In this program,int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}is similar as,int array[10] = {7, 7, 7, 7, 6, 6, 2, 2, 2, 2}.If there is any gap in the middle of the array it will be filled up by 0.In C++ above program will give the same output but it will give warnings with the output.

How to create a dynamic array of integers in C++ using the new keyword

Nitya Raut
Updated on 30-Jul-2019 22:30:25

10K+ Views

In C++, a dynamic array can be created using new keyword and can be deleted it by using delete keyword.Let us consider a simple example of it.Example Code Live Demo#include using namespace std; int main() {    int i,n;    cout

Advertisements