
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Quickly Swap Two Arrays of the Same Size in C++
In this tutorial, we will be discussing a program to understand how to quickly swap two arrays of same size in C++.
For this we will be using a quick method called std::swap() for swapping the elements of the two given arrays.
Example
#include <iostream> #include <utility> using namespace std; int main (){ int a[] = {1, 2, 3, 4}; int b[] = {5, 6, 7, 8}; int n = sizeof(a)/sizeof(a[0]); swap(a, b); cout << "a[] = "; for (int i=0; i<n; i++) cout << a[i] << ", "; cout << "\nb[] = "; for (int i=0; i<n; i++) cout << b[i] << ", "; return 0; }
Output
a[] = 5, 6, 7, 8, b[] = 1, 2, 3, 4,
Advertisements