Search
 
SCRIPT & CODE EXAMPLE
 

CPP

swap values in array c++

#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
     int array[]={1, 2};
     cout<<array[0]<<" "<<array[1]<<endl;
     swap(array[0], array[1]);
     cout<<array[0]<<" "<<array[1];
     return 0;
}
Comment

swap elements array c++

// Funtion that swaps array values
void swap(int &a, int &b){
  int temp = a;
  a = b;
  b = temp;
}
// the "&" is very important, since arrays are not mutable we 
// use this operator to swap memory addresses
Comment

PREVIOUS NEXT
Code Example
Cpp :: clang cpp compile command 
Cpp :: string count occurrences c++ 
Cpp :: removing a character from a string in c++ 
Cpp :: findung the mode in c++ 
Cpp :: c++ random 
Cpp :: all of the stars lyrics 
Cpp :: c++ user input 
Cpp :: bit c++ 
Cpp :: c++ mst kruskal 
Cpp :: c++ shared pointer 
Cpp :: std distance c++ 
Cpp :: c++ infinite for loop 
Cpp :: c++ print vector without loop 
Cpp :: how to open and read text files in c++ 
Cpp :: c++ char to uppercase 
Cpp :: use ::begin(WiFiClient, url) 
Cpp :: cpp unions 
Cpp :: copy a part of a vector in another in c++ 
Cpp :: how to get size of char array in c++ 
Cpp :: error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 
Cpp :: change integer to string c++ 
Cpp :: less than operator overloading in c++ 
Cpp :: How to find the suarray with maximum sum using divide and conquer 
Cpp :: round double to 2 decimal places c++ 
Cpp :: hamming distance c++ 
Cpp :: c++ structure 
Cpp :: how to dynamically allocate an array c++ 
Cpp :: c++ add two matrix 
Cpp :: case label in c++ 
Cpp :: c++ pass array to a function 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =