C++ Articles - Page 670 of 719

What is an arrow operator, `->` in C++?

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

512 Views

The dot and arrow operator are both used in C++ to access the members of a class or structure. They are just used in different scenarios. In C++, types declared as class, struct, or union are considered "of class type". So the following refers to all three of them.a.b is only used if b is a member of the object (or reference to an object) a. So for a.b, a will always be an actual object (or a reference to an object) of a class.a->b is essentially a shorthand notation for (*a).b, i.e., if a is a pointer to an ... Read More

What's the difference between assignment operator and copy constructor in C++?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

742 Views

The Copy constructor and the assignment operators are used to initialize one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space. It uses reference variable to point to the previous memory block.Copy Constructor (Syntax)classname (const classname &obj) { // body of constructor }Assignment Operator (Syntax)classname Ob1, Ob2; Ob2 = Ob1;Let us see the detailed differences between Copy constructor and Assignment Operator.Copy ConstructorAssignment OperatorThe Copy constructor is basically an overloaded constructorAssignment operator is basically ... Read More

Pre & post increment operator behavior in C, C++, Java, and C#

George John
Updated on 30-Jul-2019 22:30:25

4K+ Views

The Pre increment and post increment both operators are used as increment operations. The pre increment operator is used to increment the value of some variable before using it in an expression. In the pre increment the value is incremented at first, then used inside the expression.if the expression is a = ++b; and b is holding 5 at first, then a will hold 6. Because increase b by 1, then set the value of a with it.Example Code#include using namespace std; main () { int a, b = 15; a = ++b; cout

How do I use the conditional operator in C/C++?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

306 Views

This conditional operator is also known as the Ternary Operator. This operator has three phase.Exp1 ? Exp2 : Exp3;where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.The ? is called a ternary operator because it requires three operands and can be used to replace if-else statements, which ... Read More

C++ Program to Find Transpose of a Graph Matrix

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

360 Views

In this program we take a matrix and prints the transpose of the matrix. In a transpose matrix, rows become columns and vice versa.AlgorithmBegin Take number of rows and columns of the matrix. Take The elements of the matrix and stored in the matrix ‘A’. The transpose matrix is found by exchanging the rows with columns and columns with rows. Print both the original matrix and the transpose. End.Example Code#include using namespace std; int main () {    int A[10][10], a, b, i, j;    cout > a>> b;    cout > A[i][j];       cout

C++ Program to Represent Graph Using 2D Arrays

Ravi Ranjan
Updated on 26-May-2025 18:32:31

2K+ Views

The graph can be represented using various ways. One of the technique is to use a 2D array, also known as adjacency matrix. The adjacency matrix is a square matrix of size V x V to represent a finite graph data structure using a 2D array, where V is the number of nodes/vertex of the graph. In an undirected non-weighted graph, if there is an edge between vertex i and vertex j, the value at matrix[i][j] is 1 and if no edge exists, the value is 0. In this article, our task is to represent the graph using 2D arrays. ... Read More

C++ Program to Represent Graph Using Incidence List

George John
Updated on 30-Jul-2019 22:30:25

335 Views

This program represents a graph using incidence list and the time complexity of this algorithm is O(e).AlgorithmBegin Take the input of the number of vertex ‘v’ and edges ‘e’ and also take the input of ‘e’ pairs of vertexes of the given graph in e[][]. For each edge print the corresponding vertex involved in that connection. EndExample Code#include using namespace std; int main() { int i, v, e, j, c; coutv; coute; int edge[e][2]; for(i = 0; i < e; i++) { cout

C++ Program to Generate a Graph for a Given Fixed Degree Sequence

Ravi Ranjan
Updated on 29-May-2025 19:18:23

347 Views

In this article, we will understand how to generate a graph for a given fixed-degree sequence. The degree of each node is given in the form of an array. The graph generated will be an undirected graph where the degree of each node will correspond to the given degree array. Example The following example generates a graph and adjacency matrix for the given degree sequence: Input: Degree Sequence: {3, 2, 3, 2} => Degree(Node a) = 3, Degree(Node b) = 2, Degree(Node c) = 3, Degree(Node d) = 2 ... Read More

C++ Program to Generate a Random Directed Acyclic Graph (DAG) for a Given Number of Edges

Ravi Ranjan
Updated on 26-May-2025 18:29:29

709 Views

A Directed Acyclic Graph or DAG is a directed graph (a graph where each edge has a direction) that has no cycles present i.e. if we traverse along the direction of the edges then no closed loops are formed along any path. A DAG is always topologically ordered, i.e. for each edge in the graph, the start vertex of the edge(u) occurs earlier in the sequence than the ending vertex(v) of the edge i.e.(u {B C} B -> {Isolated Vertex} C -> {Isolated Vertex} D -> {B E} E -> {C} Steps to Generate Random Directed Acyclic Graph The ... Read More

C++ Program to Find the peak element of an array using Binary Search approach

Ravi Ranjan
Updated on 04-Jun-2025 18:42:56

1K+ Views

The peak element in an array is an element that is greater than its neighbor elements i.e., its left and right element. If the peak element is the starting element, then it should be greater than the next element (second element). If the peak element is the last element, then it should be greater than its previous value i.e., the second last element. In this article, we have an array of integers. Our task is to use the binary search algorithm to find the peak element present in the given array. Example Here are two examples to understand the ... Read More

Advertisements