Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
C++ Articles
Page 280 of 597
Comparison of Exception Handling in C++ and Java
The exception handling feature is present almost any object oriented languages nowadays. In C++ and Java also we can get this kind of feature. There are some similarities between exception handling in C++ and exception handling in Java, like in both languages we have to use the try-catch block. Though there are some difficulties also. These are like below −In C++, we can throw any type of data as exception. Any type of data means primitive datatypes and pointers also. In Java we can only throw the throwable objects. Subclasses of any throwable class will also be throwable.Example#include using ...
Read MoreShuffle vs random_shuffle in C++
Here we will see the Shuffle and random_shuffle in C++. Let us see the random_shuffle first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example#include using namespace std; int myRandomGenerator(int j) { return rand() % j; } main() { srand(unsigned(time(0))); vector arr; for (int j ...
Read MoreBind function and placeholders in C++
Here we will see the Bind function and the placeholders in C++. Sometimes we need to manipulate the operation of some functions as we need. We can use some default parameters to get some essence of manipulating.In C++11, one new feature is introduced, called the bind function. This helps us to do such manipulating in some easier fashion. To use these features, we have to use header file.Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs.Placeholders are namespaces which detect the position of a ...
Read MoreC++ Program to Perform Searching Based on Locality of Reference
Searching based on locality of reference, depends upon the memory access pattern data elements are reallocated.Here linear searching method is used to search an element.AlgorithmBegin int find(int *intarray, int n, int item) intialize comparisons = 0 for i = 0 to n-1 Increase comparisons if(item == intarray[i]) Print element with its index break if(i == n-1) Print element not found return -1 Print Total comparisons For j = i till i>0 intarray[j] = intarray[j-1] intarray[0] = ...
Read MoreHow to use use an array of pointers (Jagged) in C/C++?
Let us consider the following example, which uses an array of 3 integers −In CExample#include const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { printf("Value of var[%d] = %d", i, var[i] ); } return 0; }OutputValue of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200In C++Example#include using namespace std; const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { cout
Read MoreRemoving an element from C++ std::vector<> by index?
Remove an element from C++ std::vector by index can be done by following way −Example#include #include using namespace std; int main() { vector v; //declare vector //insert elements into vector v.push_back(-10); v.push_back(7); v.push_back(6); // Deletes the first element (v[0]) v.erase(v.begin() ); for (int i = 0; i < v.size(); i++) cout
Read MoreHow to declaring pointer variables in C/C++?
A pointer is used to store the address of the variables. To declare pointer variables in C/C++, an asterisk (*) used before its name.Declaration*pointer_nameIn CExample#include int main() { // A normal integer variable int a = 7; // A pointer variable that holds address of a. int *p = &a; // Value stored is value of variable "a" printf("Value of Variable : %d", *p); //it will print the address of the variable "a" printf("Address of Variable : %p", p); // reassign the value. *p = 6; printf("Value of ...
Read MorePointers, smart pointers and shared pointers in C++
PointersPointers are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer = variable name;Functionspointers are used to store address of variable.pointers can have a null value assigned.pointer can be referenced by pass by reference.a pointer has its own memory address and size on the stack.Example#include using namespace std; int main() { // A normal integer variable int a = 7; // A pointer variable that holds address of a. int *p = &a; // Value stored is value of variable "a" cout
Read MoreRAII and smart pointers in C++
RAII in C++RAII (Resource Acquisition Is Initialization) is C++ technique which control the life cycle of resource. It is a class variant and is tied to object life time.It encapsulates several resources into class where resource allocation is done by constructor during object creation and resource deallocation is done by destructor during object destruction.Resource is guaranteed to be held till the object is alive.Examplevoid file_write { Static mutex m; //mutex to protect file access lock_guard lock(m); //lock mutex before accessing file ofstream file("a.txt"); if (!file.is_open()) //if file is not open throw runtime_error("unable to open file"); ...
Read MoreWhy do we pass a Pointer by Reference in C++?
If we need to modify a pointer rather than the object that the pointer is pointing to, we pass a pointer by reference.Here is an example of how to pass a pointer by reference −Example#include using namespace std; void Decrement( int*& d ) { --d; } int main( void ) { int a = 26; int* ptr = &a; // pointer to pass // print before decrement cout
Read More