
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to 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,
- Related Articles
- C/C++ Program for Median of two sorted arrays of same size?
- How to swap two arrays without using temporary variable in C language?
- Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
- C++ code to count number of operations to make two arrays same
- How to merge two object arrays of different size by key in JavaScript
- How to know if two arrays have the same values in JavaScript?
- How to concatenate Two Arrays in C#?
- How to compare two arrays in C#?
- Swap two numbers in C#
- C++ Program to Swap Two Numbers
- C program to swap two strings
- Count distinct pairs from two arrays having same sum of digits in C++
- Maximum array from two given arrays keeping order same in C++
- How to Swap Two Numbers in Golang?
- How to swap two variables in JavaScript?

Advertisements