Found 7401 Articles for C++

How to sum up elements of a C++ vector?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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

How to shuffle a std::vector in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

How to initialize a vector in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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

How to append a vector in a vector in C++?

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

How does a vector work in C++?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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

Getting a subvector from a vector in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

C++ Program to Compute Cross Product of Two Vectors

karthikeya Boyini
Updated on 26-Feb-2020 09:17:44

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

Binary search in sorted vector of pairs in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

Exception handling and object destruction in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

Catching base and derived classes exceptions in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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

Advertisements