C++ is among the most generally used programming languages. It is utilized by hundreds of thousands of programmers each day and is essentially the most most well-liked language for aggressive programming.
Right here, we’ll record 11 C++ code snippets that may make it easier to together with your on a regular basis programming issues. So, with out additional ado, let’s get began.
1. Discover the Measurement of a Vector
You will discover the dimensions of a vector utilizing the dimension() operate.
#embody <bits/stdc++.h>
utilizing namespace std;int essential()
vector <int> arr1 = 1, 2, 3, 4;
vector <int> arr2 = ;
vector <float> arr3 = 1.2, 3.8, 3.0, 2.7, 6.6;
cout << "Measurement of arr1: " << arr1.dimension() << endl;
cout << "Measurement of arr2: " << arr2.dimension() << endl;
cout << "Measurement of arr3: " << arr3.dimension() << endl;
return 0;
Output:
Measurement of arr1: 4
Measurement of arr2: 0
Measurement of arr3: 5
2. Shuffle an Array
You’ll be able to shuffle an array in C++ utilizing the shuffle() operate.
#embody <bits/stdc++.h>
utilizing namespace std;int essential()
vector <int> arr = 1, 2, 3, 4;
unsigned seed = 0;
cout << "Unique array:";
for (int ele: arr)
cout << ele << " ";
cout << endl;
shuffle(arr.start(), arr.finish(), default_random_engine(seed));
cout << "Shuffled array:";
for (int ele: arr)
cout << ele << " ";
return 0;
Output:
Unique array:1 2 3 4
Shuffled array:2 3 1 4
3. Swap Two Variables in C++
You’ll be able to swap two variables in C++ through the use of the built-in swap() operate of the C++ STL library.
#embody <bits/stdc++.h>
utilizing namespace std;int essential()
int x = 5, y = 10;
string str1 = "MakeUseOf", str2 = "MUO";
cout << "Earlier than Swapping: " << endl;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
swap(x, y);
swap(str1, str2);
cout << "After Swapping: " << endl;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "str1: " << str1 << endl;
cout << "str2: " << str2 << endl;
return 0;
Output:
Earlier than Swapping:
x: 5
y: 10
str1: MakeUseOf
str2: MUO
After Swapping:
x: 10
y: 5
str1: MUO
str2: MakeUseOf
4. Discover the Sum of Digits of a Quantity
You will discover the sum of digits of a quantity utilizing the next course of:
- Initialize a sum variable to retailer the consequence.
- Discover the rest of the quantity by performing the modulus operation with 10.
- Add the rest with the sum.
- Divide the quantity by 10.
- Repeat the method from step 2 whereas the quantity is bigger than 10.
#embody <bits/stdc++.h>
utilizing namespace std;int essential()
int num = 4635, sum = 0, temp;
whereas (num != 0)
temp = numpercent10;
sum = sum+temp;
num = num/10;
cout << "Sum: " << sum << endl;
return 0;
Output:
Sum: 18
5. Copy a Vector to One other Vector
There are a number of methods to repeat a vector to a different vector in C++. You should utilize the task operator or go the vector as a constructor to do the identical.
#embody <bits/stdc++.h>
utilizing namespace std;void printVector(vector <int> vec)
for (auto ele: vec)
cout << ele << " ";
cout << endl;
int essential()
vector <int> vec = 1, 2, 3, 4, 5;
printVector(vec);
vector <int> newVec1 = vec;
printVector(newVec1);
vector <int> newVec2(vec);
printVector(newVec2);
return 0;
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
6. Discover the Most and Minimal Components of an Array
You will discover the utmost and minimal parts from an array utilizing the max_element() and min_element() features, respectively.
#embody <bits/stdc++.h>
utilizing namespace std;int essential()
int arr[] = 23, 56, 87, 12, 56;
int dimension = sizeof(arr)/sizeof(arr[0]);
cout << "Max factor: " << *max_element(arr, arr+dimension) << endl;
cout << "Min factor: " << *min_element(arr, arr+dimension) << endl;
return 0;
Output:
Max factor: 87
Min factor: 12
7. Insert Components in a Set
You’ll be able to insert parts in a set utilizing the insert() operate. This operate accepts the factor as a parameter that will probably be inserted within the set.
#embody <bits/stdc++.h>
utilizing namespace std;int essential()
set<string> st;
st.insert("Make");
st.insert("Use");
st.insert("Of");
st.insert("Of");
for (auto it = st.start(); it != st.finish(); it++)
cout << *it << " ";
return 0;
Output:
Make Of Use
8. Take away Duplicate From String
You’ll be able to take away the duplicate characters from a string utilizing the next methodology:
#embody <bits/stdc++.h>
utilizing namespace std;void removeDuplicateCharacters(char str[], int dimension)
int newIndex=0;
for (int i = 0; i < dimension; i++)
int j;
for (j = 0; j < i; j++)
if (str[i] == str[j])
break;
if (j == i)
str[newIndex++] = str[i];
str[newIndex] = '