Found 7346 Articles for C++

Difference between std::vector and std::array in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

22K+ Views

The following are the differences between vector and array −Vector is a sequential container to store elements and not index based.Array stores a fixed-size sequential collection of elements of the same type and it is index based.Vector is dynamic in nature so, size increases with insertion of elements.As array is fixed size, once initialized can’t be resized.Vector occupies more memory.Array is memory efficient data structure.Vector takes more time in accessing elements.Array access elements in constant time irrespective of their location as elements are arranged in a contiguous memory allocation.Vectors and arrays can be declared with the following syntax −Vector declaration:vectorarray ... Read More

Difference between namespace and class in C++

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

5K+ Views

In this section we will see what are the differences between namespace and class in C++. The namespace and classes are two different concepts. Classes are datatypes. Classes are basically extended version of structures. Classes can contain data members and functions as members, but namespaces can contain variables and functions by grouping them into one.The namespaces cannot be created as objects. This concept is used as additional information to differentiate similar functions, classes, variables etc. Variables, functions with same name can be placed in different namespaces.Now let us point out some important differences of namespaces and classes.The namespaces are used ... Read More

C++ Program to implement Gift Wrapping Algorithm in Two Dimensions

Anvi Jain
Updated on 30-Jul-2019 22:30:25

398 Views

We shall develop a C++ program to implement Gift Wrapping Algorithm in two dimensions. The Gift Wrapping Algorithm is an algorithm for computing the convex hull of a given set of points.AlgorithmBegin    function convexHull() to find convex hull of a set of n points:    There must be at least three points.    Initialize the result.    Find the leftmost point.    Starting from leftmost point, keep moving counterclockwise    until reach the start point again.    Print the result. EndExample Code Live Demo#include using namespace std; #define INF 10000 struct P {    int x;    int y; ... Read More

C++ Program to Check if a Point d lies inside or outside a circle defined by Points a, b, c in a Plane

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

206 Views

We shall consider a C++ Program to check if a point d lies inside or outside a circle defined by points a, b, c in a plane by using equations = (x-xt)^2 + (y-yt)^2 – r*rWhere, For any point t (xt, yt) on the plane, its position with respect to the circle defined by 3 points (x1, y1), (x2, y2), (x3, y3).for s < 0, t lies inside the circle.For s >0, t lies outside the circle.For s = 0, t lies on the circle.AlgorithmBegin    Take the points at input.    Declare constant L = 0 and H = ... Read More

C++ Program to use above below primitive to test whether two lines intersect

Anvi Jain
Updated on 30-Jul-2019 22:30:25

168 Views

Here is a C++ program to use above below primitive to test whether two lines intersect. It can be used to test whether a line intersects a line segment. It does if and only if one endpoint of the segment is to the left of the line and the other is to the right.AlgorithmBegin    For generating equation of the first line, generate random numbers for coefficient of x and y by using rand at every time of compilation.    For generating equation of the second line, generate random numbers for coefficient of x and y by using rand at ... Read More

C++ Program to implement Slicker Algorithm that avoids Triangulation to find Area of a Polygon

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

85 Views

Here is a C++ program to find the area of polygon using slicker algorithm that avoids Triangulation to find area of a polygon.It assumes the usual mathematical convention that positive y points upwards. In computer systems where positive y is downward, the easiest thing to do is to list the vertices counter-clockwise using the “positive y down” coordinates. Two effects then canceled out to produce a positive area.Functions and pseudocodeAlgorithmBegin    function Area() is used to calculate area of a polygon take the polygon p as argument.    for i = 0 to p.n-1       initialize j = ... Read More

C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line

Anvi Jain
Updated on 30-Jul-2019 22:30:25

373 Views

This is a C++ program to apply Above-Below-on Test to find the position of a point with respect to a Line. For any point t (xt, yt) on the plane, its position with respect to the line L connecting m and n is found by calculating the scalar s −Y = A xt + B yt + CIf Y< 0, t lies in the clockwise halfplane of L; if Y>0, t lies on the counter-clockwise halfplane; if Y= 0, t lies on L.AlgorithmBegin    Take the points as input.    For generating equation of the line, generate random numbers for ... Read More

Template Metaprogramming in C++

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

223 Views

When we write programs to do computation at compile time using a template, that is called Template Metaprogramming.Example Code#include using namespace std; templatestruct power {    enum { value = 4*power::value }; }; templatestruct power {    enum { value = 1 }; }; int main() {    cout

Variadic function templates in C++

Anvi Jain
Updated on 30-Jul-2019 22:30:25

230 Views

Variadic function templates in C++ is a function which can take a multiple number of arguments.Syntaxtemplate(typename arg, typename... args) return_type function_name(arg var1, args... var2)Example Code Live Demo#include using namespace std; void show() //base case. {    cout

Default virtual behavior in C++ vs Java

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

124 Views

In C++ methods are non-virtual by default. They can be made virtual function by using a virtual keyword.Example Code#include using namespace std; class B {    public: void s() //non virtual by default. Use virtual before the function to print “In Derived” {       cout

Advertisements