- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7401 Articles for C++
17K+ Views
Sum up of all elements of a C++ vector can be very easily done by std::accumulate method. It is defined in header. It accumulates all the values present specified in the vector to the specified sum.AlgorithmBegin Declare v of vector type. Initialize some values into v vector in array pattern. Print “Sum of all the elements are:”. Call accumulate(v.begin(),v.end(),0) to calculate the sum of all values of v vector. Print the result of sum. End.Example Code Live Demo#include #include #include using namespace std; int main() { vector v = {2,7,6,10}; cout
1K+ Views
A vector shuffle can be done in the Fisher-Yates shuffle algorithm.In this algorithm, a linear scan of a vector is done and then swap each element with a random element among all the remaining element, including the element itself.AlgorithmBegin Declare a function show(). Pass a constructor of a vector as a parameter within show() function. for (auto const& i: input) Print the value of variable i. Declare v of vector type. Initialize some values into v vector in array pattern. ... Read More
975 Views
Initialization vector can be done in many ways1) Initialize a vector by push_back() methodAlgorithmBegin Declare v of vector type. Call push_back() function to insert values into vector v. Print “Vector elements:”. for (int a : v) print all the elements of variable a. End.Example Live Demo#include #include using namespace std; int main() { vector v; v.push_back(6); v.push_back(7); v.push_back(10); v.push_back(12); cout
14K+ Views
To append a vector in a vector can simply be done by vector insert() method.AlgorithmBegin Declare a function show(). Pass a constructor of a vector as a parameter within show() function. for (auto const& i: input) Print the value of variable i. Declare vect1 of vector type. Initialize values in the vect1. Declare vect2 of vector type. Initialize values in the vect2. Call vect2.insert(vect2.begin(), vect1.begin(), vect1.end()) to append the vect1 into vect2. Print “Resultant vector ... Read More
231 Views
Vectors have the ability to resize itself automatically like dynamic arrays when an element is inserted or deleted, the container handle their storage automatically. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. Data can be inserted or erased at the begin, middle or end of the vector.This is a C++ program to implement various functions of the vector.AlgorithmBegin Declare a variable v of vector type. Declare another variable it as iterator of vector type. Declare another two variable c and i to the ineger datatype. while (1) ... Read More
4K+ Views
This is a C++ program for getting a subvector from a vector in C++AlgorithmBegin Declare s as vector s(vector const &v, int m, int n) to initialize start and end position of vector to constructor. auto first = v.begin() + m. auto last = v.begin() + n + 1. Declare a variable vector of vector type. Pass the value of first and last position of vector. Return vector. Declare a template T. Declare a function show(). ... Read More
6K+ Views
This is a C++ program to compute Cross Product of Two Vectors.Let us suppose, M = m1 * i + m2 * j + m3 * kN = n1 * i + n2 * j + n3 * k.So, cross product = (m2 * n3 – m3 * n2) * i + (m1 * n3 – m3 * n1) * j + (m1 * n1 – m2 * n1) * kwhere m2 * n3 – m3 * n2, m1 * n3 – m3 * n1 and m1 * n1 – m2 * n1 are the coefficients of unit vector along ... Read More
363 Views
This is a C++ program to implement Binary search in a sorted vector of pairs.AlgorithmBegin Declare a structure keycompare. Function operator()(const pair& v, const int& k) returns Booleans. Status = v.first < k. Return status. Function operator()(const pair& v, const int& k) returns Booleans. Status = k < v.first. Return status. Declare a vector v. Declare key and value pair within v of the integer datatype. Call push_back() ... Read More
614 Views
Destructors in C++ basically called when objects will get destroyed and release memory from the system. When an exception is thrown in the class, the destructor is called automatically before the catch block gets executed.AlgorithmBegin Declare a class sample1. Declare a constructor of sample1. Print “Construct an Object of sample1” Declare a destructor of sample1. Print “Destruct an Object of sample1” Declare a class sample. Declare a constructor of sample2. Declare variable i of the integer datatype. ... Read More
1K+ Views
To catch an exception for both base and derive class then we need to put catch block of derived class before the base class. Otherwise, the catch block of derived class will never be reached.AlgorithmBegin Declare a class B. Declare another class D which inherits class B. Declare an object of class D. Try: throw derived. Catch (D derived) Print “Caught Derived Exception”. Catch (B b) Print “Caught Base Exception”. End.Here is a simple example where catch of derived class has been placed before the catch of base ... Read More