Found 7197 Articles for C++

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

Revathi Satya Kondra
Updated on 02-May-2025 17:56:29

16K+ Views

In C++, a vector is like a array which can be used accordingly. If you want to combine (append) two vectors, you need to add all the elements of one vector to the end of another. This is called appending a vector in a vector. To append a vector in a vector can simply be done by vector insert() method and moreover by using loops and std::move() to transfer elements. There are different ways to append one vector into another in C++. The most common ones are: Using insert() method Using ... Read More

How does a vector work in C++?

Revathi Satya Kondra
Updated on 30-Apr-2025 18:22:16

364 Views

In C++, a vector is a dynamic array that can grow or shrink automatically. It can store elements in a row (contiguous memory) and resizes itself when needed. When it runs out of space, it creates a bigger array, copies the old data, and adds the new one. So, you can easily add, remove, or access elements using functions like push_back(), size(), and erase(). Basic Operations (push_back, access) A vector stores elements in a contiguous memory block. You can add elements using push_back() and access them using [] or at(). Syntax Following is the syntax is as follows: vector vec; ... Read More

C++ Program to Compute Cross Product of Two Vectors

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

8K+ 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++

Ravi Ranjan
Updated on 18-Aug-2025 16:08:48

601 Views

In this article, we have a sorted vector of pairs. Our task is to search for a target key using binary search in the given vector of pairs. Binary Search Algorithm The binary search algorithm works on the divide-and-conquer principle as it keeps dividing the array in half before searching. To search for an element in an array using binary search, it should be sorted. In the sorted array, we find the middle element and compare it with the element that has to be searched, and based on the comparison, we either search in the left or right sub-array or ... Read More

Exception handling and object destruction in C++

Revathi Satya Kondra
Updated on 10-Jun-2025 13:11:44

888 Views

In this article, you will learn what is exception handling, object destruction, and Handing exception thrown in Object Destructor in C++. C++ Exception Handling An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. C++ exception handling is built upon three keywords: try, catch, and throw. try: This block contains the code ... Read More

Catching base and derived classes exceptions in C++

Revathi Satya Kondra
Updated on 07-May-2025 14:33:33

2K+ Views

To catch an exception for both base and derived classes, we need to put the catch block of the derived class before the base class. Otherwise, the catch block for the derived class will never be reached. This happens because C++ follows a top-down approach when checking catch blocks. So, by placing the derived class catch block first, we ensure that specific exceptions are handled correctly before falling back on the base class. Algorithm Following is the algorithm to catch Base and Derived classes Exceptions in C++: Begin Declare a class B. Declare another ... Read More

How to declare a pointer to a function in C?

Revathi Satya Kondra
Updated on 06-May-2025 18:53:52

6K+ Views

In C, a pointer is a variable whose value is the address of another variable or memory block, i.e direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable or block address. Basic Pointer to a Function A pointer to a function is simply a variable that stores the address of a function instead of a normal data value. Syntax Following is the syntax of basic pointer to a function: Datatype *variable_name Algorithm Following is the algorithm for the basic pointer to a Function: Begin. ... Read More

vector::resize() vs vector::reserve() in C++

karthikeya Boyini
Updated on 06-May-2025 17:31:12

891 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. The main difference between vector resize() and vector reserve() is that resize() is used to change the size of vector where reserve() doesn’t. reserve() is only used to store at least the number of the specified elements without having to reallocate memory. But in resize() if the number is smaller than the current number then it resizes the memory and deletes the excess space over it. C++ vector::resize() The resize() is used to change the actual ... Read More

std::vector::resize() vs. std::vector::reserve() in C++

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

3K+ 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.The main difference between vector resize() and vector reserve() is that resize() is used to change the size of vector where reserve() doesn’t. reserve() is only used to store at least the number of the specified elements without having to reallocate memory. But in resize() if the number is smaller than the current number then it resizes the memory and deletes the excess space over it.vector::resize()Vector resize() is used to change its size.ExampleSteps in the source code:Begin ... Read More

tmpfile() function in C

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

1K+ Views

The function tmpfile() creates a temporary file in binary update mode in C. It initializes in header file of a C program. It always returns a null pointer if the temporary file cannot be created. The temporary file deleted automatically just after the termination of program.SyntaxFILE *tmpfile(void)Return valueIf file creation is successful, the function returns a stream pointer to the temporary file created. If the file cannot be created, NULL pointer is returned.AlgorithmBegin.    Declare an array variable c[] to the character datatype and take a character data string.    Initialize a integer variable i ← 0.    Declare a ... Read More

Advertisements