STL - Algorithm/Algorithm - Non-Modifying
Algorithm - adjacent_find()
Programmer.
2016. 2. 7. 15:58
소스)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include<vector> #include<iostream> #include<algorithm> bool find_adj(int i,int j) { if (i == j) return true; return false; } void main() { std::vector<int> Vector = { 1,3,3,5,7,7,9,11,13,15 }; std::vector<int>::iterator Iter; Iter = std::adjacent_find(Vector.begin(), Vector.end()); if (Iter != Vector.end()) std::cout << "first pair of repeated element ->" << *Iter << std::endl; else std::cout << "no data" << std::endl; Iter = std::adjacent_find(++Iter, Vector.end(), find_adj); if (Iter != Vector.end()) std::cout << "second pair of repeated element ->" << *Iter << std::endl; else std::cout << "no data" << std::endl; } | cs |
내용)
adjacent_find()함수는 파라메터에 따라서 다르게 작동합니다.
- adjacent_find(x,y)(17줄)
adjacent_find(x,y)함수는 이터레이터 x가 가리키는 위치부터 이터레이터 y가 가리키는 위치 사이의 데이터들중에서 중복되는 데이터가 발견되면 그 앞에 부분을 가리키는 iterator를 리턴합니다. - adjacent(x,y,z)(24줄)
adjacent_find(x,y,z)함수는 함수는 이터레이터 x가 가리키는 위치부터 이터레이터 y가 가리키는 위치 사이의 데이터들중에서 z로 true값을 던지는 부분의 앞을 가리키는 iterator를 리턴합니다.
결과)