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 :: convert letters to uppercase in c++ 
Cpp :: deep copy c++ 
Cpp :: c++ rand include 
Cpp :: cout c++ 
Cpp :: remove specific element from vector c++ 
Cpp :: Reverse Level Order Traversal cpp 
Cpp :: c++ pass array to a function 
Cpp :: conditional operator in c++ 
Cpp :: c++ insert into map 
Cpp :: methods available for a stl vector 
Cpp :: memory leak in cpp 
Cpp :: Program To Calculate Number Power Using Recursion In C++. The power number should always be positive integer. 
Cpp :: C++ Limit of Integer 
Cpp :: insert image using set atribute 
Cpp :: find in vector 
Cpp :: c++ char array size 
Cpp :: cpp string find all occurence 
Cpp :: dynamic memory c++ 
Cpp :: c++ lettura file 
Cpp :: min element in vector c++ 
Cpp :: maxheap cpp stl 
Cpp :: vector in c++ 
Cpp :: enum c++ 
Cpp :: string comparison c++ 
Cpp :: c++ forbids comparison between pointer and integer 
Cpp :: operator precedence in cpp 
Cpp :: use of strstr in c++ 
Cpp :: What is a ~ in c++ 
Cpp :: three way comparison operator c++ 
Cpp :: how to make randomizer c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =