devLupin / algorithm

PS
1 stars 0 forks source link

swap in <algorithm> #26

Open devLupin opened 1 year ago

devLupin commented 1 year ago

swap


// example

#include <iostream>
#include <algorithm>
using namespace std;

int main(){

    int a = 3, b = 5;
    cout << "a: " << a << ", b: " << b << endl;
    swap(a, b);
    cout << "a: " << a << ", b: " << b << endl;

    return 0;
}
devLupin commented 1 year ago

swap_ranges


// example

#include <iostream>
#include <algorithm>
using namespace std;

int main(){

    int a[5] = {1, 2, 3, 4, 5};
    int b[5] = {6, 7, 8, 9, 10};
    swap_ranges(a, a+3, b);

    cout << "a:";
    for(int i=0; i<5; i++) cout << ' ' << a[i];
    cout << endl << "b:";
    for(int i=0; i<5; i++) cout << ' ' << b[i];
    cout << endl;

    return 0;
}