별의 공부 블로그 🧑🏻‍💻
728x90
728x170

*vector 안의 원소들의 순서를 역순으로 바꾸는 방법


1) <algorithm> 헤더를 include하여 reverse 함수를 사용한다.

1
2
3
4
5
6
7
8
#include <vector>
#include <algorithm>
 
int main() {
  std::vector<int> a;
  std::reverse(a.begin(), a.end());
  return 0;
}
cs


2) rbegin()과 rend()를 사용한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
#include <iostream>
 
template<class InIt>
void print_range(InIt first, InIt last, char const* delim = "\n"){
  --last;
  for(; first != last; ++first){
    std::cout << *first << delim;
  }
  std::cout << *first;
}
 
int main(){
  int a[] = { 12345 };
  std::vector<int> v(a, a+5);
  print_range(v.begin(), v.end(), "->");
  std::cout << "\n=============\n";
  print_range(v.rbegin(), v.rend(), "<-");
}
cs



내용 출처 : https://stackoverflow.com/questions/8877448/how-do-i-reverse-a-c-vector

728x90
그리드형(광고전용)

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

Pair Vector  (0) 2017.11.26
[header][container] queue : priority_queue  (0) 2017.11.17
[header][container] queue : queue  (0) 2017.11.17
string형 변수 길이 구하기  (0) 2017.11.15
정렬 알고리즘의 시간 복잡도 비교  (0) 2017.11.08
데이터 형식 범위  (0) 2017.11.08
[header][C library] cwctype (wctype.h)  (0) 2017.11.08
[header][C library] cwchar (wchar.h)  (0) 2017.11.08
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖