Server Side Programming Articles - Page 2304 of 2651

What is the size of a pointer in C/C++?

Revathi Satya Kondra
Updated on 27-May-2025 17:14:33

10K+ Views

The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. Usually it depends upon the word size of underlying processor, for example for a 32 bit computer the pointer size can be 4 bytes and for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed. It is common to all data types like int *, float * etc. Depending on the system architecture, pointer size may vary. The following table shows the pointer size based on the ... Read More

What is the easiest way to initialize a std::vector with hardcoded elements in C++?

Revathi Satya Kondra
Updated on 12-Jun-2025 13:45:58

667 Views

In C++, a std::vector is a container that is a part of the STL (Standard Template Library). It enables dynamic arrays which can adjust their size automatically. Depending on the requirements and version of C++ being used, there are many ways to initialize a std::vector with hardcoded elements. Initializing a std::vector with Hardcoded Elements Some of the approaches to initialize a std::vector with hardcoded elements in C++: Using Initializer List Using assign() Function Using accumulate() function Initializing Vector Using Initializer List An initializer list is a ... Read More

Ways to copy a vector in C++

Nishu Kumari
Updated on 30-Jan-2025 14:45:41

13K+ Views

In C++, vectors are commonly used to store dynamic collections of data. Sometimes, we need to copy a vector to manipulate data without affecting the original. In this article, we'll show you the easiest ways to copy a vector in C++, along with examples to show you how it works. Ways to copy a vector in C++ There are different ways to copy a vector in C++, and each method has its own use. We'll cover the following approaches: Using std::copy Using assign() Method By assignment "=" ... Read More

Sorting a vector in descending order in C++

Aishwarya Naglot
Updated on 22-Nov-2024 11:21:24

18K+ Views

In C++, sorting a vector is a common task, and many problems are based on sorting. To sort a vector in descending order, we can use the std::sort() function from the STL (Standard Template Library). This function is included in the library. By default, std::sort() sorts in ascending order, but you can pass a custom comparator, such as std::greater(), to sort in descending order. Here’s a detailed overview of how sorting a vector works in C++ and what solutions we have provided in this article. ... Read More

Sorting a vector in C++

Nishu Kumari
Updated on 30-Jan-2025 14:46:25

19K+ Views

Sorting a vector in C++ means arranging its elements in a specific order, like ascending or descending. This is a common task when you need to organize data efficiently. C++ provides different ways to sort a vector. In this article, we will look at different ways to sort a vector in C++. Let's look at this example to better understand it: For the vector: V = {5, 3, 8, 1, 2} Sorted Output: {1, 2, 3, 5, 8} For the vector: V = {22, 23, 5, 6, 34} Sorted Output: {5, 6, 22, 23, 34} Approaches to ... Read More

Passing a vector to constructor in C++

Revathi Satya Kondra
Updated on 26-May-2025 17:24:41

1K+ Views

In C++, you can pass a std::vector to a class constructor to create a list of values when the object is created. So that the object can store or work with a list of values right from the beginning. Why do You Pass a Vector to a Constructor? Passing a Vector to a Constructor allows the object to be initialized with data at the time of creation. When you pass a vector to a function, it simplifies the code by eliminating the need to initialize the function parameters, as the vector ... Read More

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

374 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

615 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

Advertisements