[STL] swap

STL/변경 가능 시퀀스 알고리즘 2010. 5. 17. 15:44 Posted by zetz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
제너릭 swap 알고리즘은 두 개의 값을 서로 맞바꾼다기본적으로 상수 시간 복잡도를 가지며 벡터 컨테이너의 경우벡터 컨테이너의 멤버함수인swap을 사용한다.이는 대입연산을 사용하지 않고 벡터 내부 포인터를 바꿈으로서 상수시간 복잡도를 유지한다.*/
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
 
using namespace std;
int main()
{
    cout<< "Illustration the generic swap algorithm."<< endl;
    int high = 250, low = 0;
    swap(high, low);
    assert (high == 0 && low == 250);
    cout<< " --- Ok."<< endl;
    vector<int> vector1(100, 1), vector2(200, 2);
    swap(vector1, vector2);
    assert (vector1 == vector<int>(200, 2) &&
               vector2 == vector<int>(100, 1));
    return 0;
}

'STL > 변경 가능 시퀀스 알고리즘' 카테고리의 다른 글

[STL] swap_ranges  (0) 2010.05.17
[STL] transform  (0) 2010.05.17
[STL] unique  (0) 2010.05.17
[STL] search  (0) 2010.05.17
[STL] rotate  (0) 2010.05.17