Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home C++

11 C++ Code Snippets for Everyday Programming Problems

learningcode_x1mckf by learningcode_x1mckf
September 12, 2022
in C++
0
11 C++ Code Snippets for Everyday Programming Problems
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


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] = '';

int essential()

char str[] = "MakeUseOf";
int dimension = strlen(str);

cout << "Unique String: " << endl;
cout << str << endl;

removeDuplicateCharacters(str, dimension);

cout << "New String: " << endl;
cout << str << endl;
return 0;

Output:

Unique String:
MakeUseOf
New String:
MakeUsOf

9. Discover the Size of a C++ String

You will discover the size of a C++ string utilizing the size() operate. Alternatively, you too can use the dimension() operate (it is an alias of the size() operate).

#embody <bits/stdc++.h>
utilizing namespace std;

int essential()

string str1 = "MakeUseOf";
cout << "Size of " << str1 << " : " << str1.size() << endl;

string str2 = "lorem ipsum";
cout << "Size of " << str2 << " : " << str2.dimension() << endl;

return 0;

Output:

Size of MakeUseOf : 9
Size of lorem ipsum : 11

10. Delete an Component From the Array

You’ll be able to delete a component from the array utilizing the next strategy:

#embody <bits/stdc++.h>
utilizing namespace std;

int deleteElementFromArray(int arr[], int dimension, int elementToBeDeleted)

int i, j;


for (i = 0; i < dimension; i++)

if (arr[i] == elementToBeDeleted)

break;


if (i < dimension)

dimension = dimension - 1;

for (j = i; j < dimension; j++)

arr[j] = arr[j+1];


return dimension;

void printArrayElements(int arr[], int dimension)

for (int i = 0; i < dimension; i++)

cout << arr[i] << " ";

cout << endl;

int essential()

int arr[] = 1, 2, 3, 4, 5;
int dimension = sizeof(arr)/sizeof(arr[0]);

cout << "Unique Array: " << endl;
printArrayElements(arr, dimension);

int elementToBeDeleted = 3;
dimension = deleteElementFromArray(arr, dimension, elementToBeDeleted);

cout << "New array: " << endl;
printArrayElements(arr, dimension);

return 0;

Output:

Unique Array:
1 2 3 4 5
New array:
1 2 4 5

Generally it is not simple to straight perceive a posh code. It is best to comply with a number of the basic programming principles like documenting the code, refactoring, and so forth to make your code extra strong.

11. Iterate By way of a Vector

You’ll be able to iterate by a vector in a number of methods. Beneath are three of essentially the most used methods to iterate by a vector:

Utilizing vary for

#embody <bits/stdc++.h>
utilizing namespace std;

int essential()

vector <int> vec = 1, 2, 3, 4, 5;


for (auto factor: vec)

cout << factor << " ";

return 0;

Utilizing Indexing

#embody <bits/stdc++.h>
utilizing namespace std;

int essential()

vector <int> vec = 1, 2, 3, 4, 5;


for (int i = 0; i < vec.dimension(); i++)

cout << vec[i] << " ";

return 0;

Utilizing Reference of the Iterator

#embody <bits/stdc++.h>
utilizing namespace std;

int essential()

vector <int> vec = 1, 2, 3, 4, 5;


for (auto it = start(vec); it != finish(vec); it++)

cout << *it << " ";

return 0;

The above three codes will show the identical output:

You might also like

C can be memory-safe – Security Boulevard

C++ Lambda Expressions Explained | Built In

C++ creator Bjarne Stroustrup defends its safety

1 2 3 4 5

If you wish to take a look on the full supply code used on this article, here is the GitHub repository.

Make Use of C++ Code Snippets

Make use of those C++ code snippets on your on a regular basis programming issues. Whether or not you utilize C++ for writing easy applications or aggressive programming, these code snippets can turn out to be useful.

Alternatively, you must get began with Go if you wish to attempt your palms soiled with robotics, DevOps, cloud programming, knowledge science, or synthetic intelligence. Go is an open-source, easy-to-learn programming language with a number of benefits over different programming languages.



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

C can be memory-safe – Security Boulevard

by learningcode_x1mckf
February 1, 2023
0
C can be memory-safe – Security Boulevard

The concept of memory-safe languages is within the information currently. C/C++ is known for being the world’s system language (that runs most issues) but in addition notorious for being...

Read more

C++ Lambda Expressions Explained | Built In

by learningcode_x1mckf
February 1, 2023
0
C++ Lambda Expressions Explained | Built In

One of many new options launched in trendy C++ ranging from C++ 11 is the lambda expression.It's a handy solution to outline an nameless operate object or functor....

Read more

C++ creator Bjarne Stroustrup defends its safety

by learningcode_x1mckf
January 31, 2023
0
C++ creator Bjarne Stroustrup defends its safety

The creator of C++, Bjarne Stroustrup, is defending the venerable programming language after the US Nationwide Safety Company (NSA) just lately really helpful towards utilizing it. NSA advises...

Read more

Solid Sands and Rapita target hard to do C++ code analysis … – eeNews Europe

by learningcode_x1mckf
January 30, 2023
0
Solid Sands and Rapita target hard to do C++ code analysis … – eeNews Europe

Solid Sands and Rapita target hard to do C++ code analysis ...  eeNews Europe Source link

Read more

Bjarne Stroustrup Defends C++ As Safe

by learningcode_x1mckf
January 29, 2023
0

It is not stunning to search out the creator of a language defending the language they created and so it's with the newest paper from Bjarne Stroustrup. Is...

Read more
Next Post
How to write HTML in Swift?

How to write HTML in Swift?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Abstract classes vs. interfaces in Java

Abstract classes vs. interfaces in Java

January 31, 2023
Time limit for notify – JavaScript – SitePoint Forums

Including bundle.min.js inside an html app – JavaScript – SitePoint Forums

January 24, 2023
Self sizing cells with rotation support

Self sizing cells with rotation support

October 10, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?