;find

#include <algorithmn>

vector<int> v;
v.push_back(1);
v.push_back(2);

 

vector<int>::iterator iter;

iter = find(v.begin(), v.end(), 1);
if (iter != v.end())
  cout << "Element fount: " << *iter << endl;
else
  cout << "Element not found" << endl;

 

;adjacent_find // 시퀀스에서 현재 원소가 다음 원소와 같아지는 첫 원소의 반복자를 반환.

#include <algorithmn>

vector<int> v; 
v.push_back(1); 
v.push_back(2);

v.push_back(3);

v.push_back(3);

v.push_back(4);

v.push_back(5);

 

vector<int>::iterator iter;

iter = adjacent_find(v.begin(), v.end()); 

if(iter != v.end())

  cout << *iter <<endl; // *iter = 3, iter = v.begin() + 2

 

;reverse

#include <algorithmn>

string str = "abcdefg";

 

reverse(str.begin(), str.end());

cout << str << endl; // "gfedcba"

 

char arr[] = "hijklmn";

size_t len = strlen(arr);

 

reverse(&arr[0], &arr[len]);

cout << arr << endl; // "nmlkjih"

 

;merge

#include <algorithmn>

int first[] = {5,10,15,20,25};

int second[] = {50,40,30,20,10};

vector<int> v(10);

 

sort(first, first+5); // 합치기 위해선 정렬 기준이 같아야 함.

sort(second, second+5);

merge(first, first+5, second, second+5, v.begin());

 

for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter)

  cout << *iter << endl;

 

;accumulate // 구간에 속한 값 더하기

#include <numeric>

int x[5] = {1, 2, 3, 4, 5};

vector<int> v(&x[0], &x[5]);

 

int sum = accumulate(v.begin(), v.end(), 0); // 마지막 인자 값은 대상 타입에 따라 달라진다.

int multi = accumulate(v.begin(), v.end(), 1, multiplies<int>()); // 곱하기도 가능. 응용하기.

 

*https://stackoverflow.com/questions/3604478/c-stdaccumulate-doesnt-give-the-expected-sum

 

C++ std::accumulate doesn't give the expected sum

double numbers[ ] = { 1, 0.5 ,0.333333 ,0.25 ,0.2, 0.166667, 0.142857, 0.125, 0.111111, 0.1 } ; std::vector doublenumbers ( numbers , numbers + 10 ) ; std::cout...

stackoverflow.com

;sort

#include <algorithmn>

int x[5] = { 11, 2, 23, 4, 5 };
vector v(&x[0], &x[5]);
v.push_back(1);
v.push_back(3);

for (iter = v.begin(); iter != v.end(); iter++)
  cout << *iter << " ";  // 11, 2, 23, 4, 5, 1, 3

sort(v.begin(), v.end(), Func); // 기본 오름차순 정렬. Func 정렬 규칙에 따라 적용가능.
ex) bool compare(int a, int b) { return a > b; } // 내림차순 정렬.


for (iter = v.begin(); iter != v.end(); iter++)
  cout << *iter << " ";  // 1, 2, 3, 4, 5, 11, 23

 

*list의 경우는 제네릭이 아닌 멤버 함수로 sort 제공. list는 양방향 반복자. 임의 접근이 불가능.

 ex) list.sort();

 

;binary_search // 이진 탐색_반씩 쪼개서 찾기.

#include <algorithmn>

vector<int> v = {1, 2, 3, 4, 5}; // 정렬이 되어있다는 전제하에..

 

bool result = binary_search(v.begin(), v.end(), 3);

if (true == result)
  cout << "Is in here!" << endl; 
else 
  cout << "Where is it?" << endl;

 

;lower_bound, upper_bound

*이진 탐색을 이용해여 원소를 찾아낸다.

#include <algorithmn>

vector vv = { 1, 2, 3, 3, 3, 4, 5 };
bool found = binary_search(vv.begin(), vv.end(), 3); // 3을 찾는다. bool 값 반환


vector::iterator iter_lower = lower_bound(vv.begin(), vv.end(), 3); 

*lower_bound는 0번째 배열의 원소부터 찾아서 어떠한 값의 "이상이 되는 위치"를 반환.


vector::iterator iter_upper = upper_bound(vv.begin(), vv.end(), 3);

*upper_bound는 마지막 원소부터 원하는 값을 찾고 그 값이 시작되기 전의 위치를 반환.

 

;random_shuffle // 구간내의 원소들을 무작위로 뒤섞기.

#include <algorithmn>

random_shuffle(vector.begin(), vecter.end());

 

;replace

#include <algorithmn>

string s = "abcdefg";
vector v(s.begin(), s.end());

replace(v.begin(), v.end(), 'e', 'E'); // 'e'를 'E'로 치환

 

;swap

#include <algorithmn>

swap(vector1, vector2); // 두 값 맞바꿈

 

;min, max

#include <algorithmn>

vector<int>::iterator iter_min = min_element(vector.begin(), vector.end());

vector<int>::iterator iter_max = max_element(vector.begin(), vector.end());

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

std::string  (0) 2019.07.18
<iostream>  (0) 2019.06.20
STL map  (0) 2019.06.17
STL iterator  (0) 2019.06.13
STL container  (0) 2019.06.12

+ Recent posts