C++ - Algorithm 헤더 파일 reverse(), rotate(), random_shuffle()

2020. 4. 13. 16:52·Language/C++

이번에는 구간에 대한 함수들입니다. 역시 함수 이름을 참 잘 지어놔서, 이름으로 기능을 추론하기 쉽습니다.

 

[reverse(arr_begin,arr_end) : 주어진 구간을 뒤집습니다.]

#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};
	reverse(a, a+5);
	reverse(b+1, b+4);

	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;
}
a: 5 4 3 2 1
b: 6 9 8 7 10

 

[rotate(arr_begin,how_many_rotate,arr_end) : 구간의 값들을 모두 여러 칸씩 한쪽으로 땡긴 뒤, 튀어나온 값은 다시 반대쪽으로 보내는 연산.]

#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};
	rotate(a, a+2, a+5);
	rotate(b+1, b+2, b+4);

	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;
}
a: 3 4 5 1 2
b: 6 8 9 7 10

 

[random_shuffle(arr_begin,arr_end) : 주어진 구간을 랜덤하게 섞는 함수]

#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};
	random_shuffle(a, a+5);
	random_shuffle(b+1, b+4);

	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;
}
a: 5 2 4 3 1
b: 6 7 9 8 10

[reverse(arr_begin,arr_end) : 역으로 뒤집는 함수.]

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

int main(){

	stringstream ss;
	string line;
	getline(cin, line);
	ss.str(line);

	string word;
	while(ss >> word){
		reverse(word.begin(), word.end());
		cout << word;
		if(ss.eof()) cout << endl;
		else cout << ' ';
	}

	return 0;
}

 일단 한 줄을 getline() 함수로 입력받고, 스트링스트림에 넣은 후, 스트링스트림이 끝날 때까지 매번 문자열을(단어를) 다시 입력받습니다.

그리고 문자열에 reverse() 함수를 적용하고 출력합니다. 스트링스트립의 끝에서는 한 줄 띄어 줍니다.

I love computer science[엔터]
I evol retupmoc ecneics
[커서]

이번엔 철자가 아니라, 단어를 거꾸로 쓸 겁니다!!

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

	stringstream ss;
	string line;
	getline(cin, line);
	ss.str(line);
    
	string word;
	vector<string> str;
	while(ss >> word){
		str.push_back(word);
	}

	reverse(str.begin(), str.end());
	for(int i=0; i<str.size(); i++){
		cout << str[i];
    
		if(i == str.size()-1) cout << endl;
		else cout << ' ';
	}

	return 0;
}

 

이번엔 모든 단어를 기억할 필요가 있기 때문에 벡터까지 동원합니다. string형 벡터죠. 거기에 단어 하나하나를 다 집어넣은 후

그 벡터를 reverse() 함수로 뒤집으면 단어 단위로 순서가 바뀌겠죠. 그리고 원소를 하나씩 출력합니다.!!

I love computer science[엔터]
science computer love I
[커서]

 

출처: https://m.blog.naver.com/PostList.nhn?blogId=kks227&categoryNo=163&logCode=0

'Language > C++' 카테고리의 다른 글

C++ - Algorithm 헤더 파일 binary_search(), lower_bound(), upper_bound()  (0) 2020.04.13
C++ - Algorithm 헤더 파일 replace(), replace_if(), remove(), remove_if()  (0) 2020.04.13
C++ - Algorithm 헤더 파일 - swap(), swap_ranges(), copy(), fill()  (0) 2020.04.13
C++ - vector 초기화  (0) 2020.04.13
C++ - 형변환  (0) 2020.04.05
'Language/C++' 카테고리의 다른 글
  • C++ - Algorithm 헤더 파일 binary_search(), lower_bound(), upper_bound()
  • C++ - Algorithm 헤더 파일 replace(), replace_if(), remove(), remove_if()
  • C++ - Algorithm 헤더 파일 - swap(), swap_ranges(), copy(), fill()
  • C++ - vector 초기화
dev.wookii
dev.wookii
Effort Maketh Happiness
  • dev.wookii
    습관처럼
    dev.wookii
  • 전체
    오늘
    어제
    • 분류 전체보기 (295)
      • Language (35)
        • python (13)
        • C++ (22)
      • Kaggle (4)
      • Algorithms (112)
        • BOJ (58)
        • programmers (43)
        • SWExpertAcademy (2)
      • Certification (38)
        • Adsp (0)
        • Sqld (28)
        • 정처기 (9)
        • 빅데이터 분석기사 (0)
      • Data Analysis & ML (6)
      • 금융 & 디지털 (65)
      • CS (32)
        • DB (2)
        • SE (3)
        • Web&JSP (1)
        • Network (11)
        • OS (2)
        • Linux&Unix (6)
        • Server (1)
        • UX,UI (1)
        • 보안 (5)
      • 기타 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Ebay korea #coding test
    funny algorithms
    programmers
    시뮬레이션
    2020 KAKAO
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dev.wookii
C++ - Algorithm 헤더 파일 reverse(), rotate(), random_shuffle()
상단으로

티스토리툴바