Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ 팁&트릭
[46] [STL]원소 순서 변경 알고리듬 정리 / rotate_copy 알고리듬 소개
김백일.cedar [cedar] 27741 읽음    2003-04-09 19:51
이제까지 원소의 순서를 변경하는 몇가지 알고리듬을 소개했습니다.
잠깐 정리하고 넘어가도록 하죠. 번호는 게시판의 글 번호입니다.

6) random_shuffle: 원소를 임의로 뒤섞기
41) random_sample, ramdom_sample_n: 임의 추출
14) sort: 모든 원소 정렬
49) partial_sort: 일부만 정렬
14) partition, stable_partition: 조건을 만족하는 원소를 앞쪽으로 이동
23) nth_element: 정렬했을 경우에 n번째 원소가 위치가 놓이는 위치로 놓고 시퀀스를 파티션
21, 49) pop_heap, push_heap, make_heap, sort_heap: 힙 연산
45) next_permutation, prev_permutation : 순열 계산

이 외에 아직 다루지 않은 것으로 rotate, rotate_copy, reverse, reverse_copy가 있습니다.
오늘은 이중에서 rotate_copy의 예제를 들어보죠. 이것만 보면 나머지의 사용법도 금방 아실 수 있겠죠?

------------------------------------------------------------------------------------------

: 다음과 같이 N 과 M을 입력받아

: N = 1
: M = 5

: 다음과 같이 출력하는 프로그램을 작성하라.

:  1 2 3 4 5 
:  5 1 2 3 4
:  4 5 1 2 3
:  3 4 5 1 2
:  2 3 4 5 1

//---------------------------------------------------------------------------
#include <iostream>
#pragma hdrstop
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
//---------------------------------------------------------------------------

using namespace std;

int main()
{
    int N, M;
    cout << "N = "; cin >> N;
    cout << "M = "; cin >> M;

    vector<int> series(M);
    iota(series.begin(), series.end(), N);

    ostream_iterator<int> out(cout, " ");
    copy(series.begin(), series.end(), out);
    cout.put('\n');

    for (int i = 1; i < M; ++i) {
        rotate_copy(series.begin(), series.end() - i, series.end(), out);
        cout.put('\n');
    }

    return 0;
}

+ -

관련 글 리스트
46 [STL]원소 순서 변경 알고리듬 정리 / rotate_copy 알고리듬 소개 김백일.cedar 27741 2003/04/09
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.