관리 메뉴

Kim's Programming

Algorithm - random_shuffle() 본문

STL - Algorithm/Algorithm - Modifying

Algorithm - random_shuffle()

Programmer. 2017. 7. 13. 14:29

원형)


1
2
3
4
5
6
7
8
//기본형
template <class RandomAccessIterator>
  void random_shuffle (RandomAccessIterator first, RandomAccessIterator last);
 
//사용자 정의형
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
                       RandomNumberGenerator&& gen);
cs



의미)


[Iterator first, Iterator last)사이에 있는 데이터들을 랜덤하게 재정렬합니다.



소스)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
#include<algorithm>
#include<vector>
 
void Print(const std::vector<int>& target)
{
    for (std::vector<int>::const_iterator iterPos = target.begin(); iterPos != target.cend(); iterPos++)
        std::cout << *iterPos << ' ';
    std::cout << std::endl;
}
 
void main()
{
    std::vector<int> vector = { 1,3,5,4,3,2,6,7,9,11 };
    std::vector<int> vectorCopy(10);
    std::cout << "Vector--->"; Print(vector);
    std::random_shuffle(vector.begin(), vector.end());
    std::cout << "Vector--->"; Print(vector);
}
cs



리턴값)


없음



결과)




'STL - Algorithm > Algorithm - Modifying' 카테고리의 다른 글

Algorithm - shuffle()  (0) 2017.07.13
Algorithm - rotate_copy()  (0) 2017.07.13
Algorithm - rotate()  (0) 2017.07.13
Algorithm - reverse_copy()  (0) 2017.07.13
Algorithm - reverse()  (0) 2017.07.13