STL - Algorithm/Algorithm - Non-Modifying
Algorithm - search()
Programmer.
2016. 2. 11. 16:12
소스)
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 31 | #include<iostream> #include<algorithm> #include<vector> bool search_bool(int i, int j) { if (i == j) return true; return false; } int main() { std::vector<int> Vector = { 1,3,5,7,9,11,13,15 }; int Array[] = { 7,9,11 }; std::vector<int>::iterator Iter; Iter = std::search(Vector.begin(), Vector.end(), Array, Array + 3); if (Iter != Vector.end()) std::cout << "Array Position -->" << Iter - Vector.begin() << std::endl; else std::cout << "not found" << std::endl; int Array2[] = { 3,5,7 }; Iter = std::search(Vector.begin(), Vector.end(), Array2, Array2 + 3,search_bool); if (Iter != Vector.end()) std::cout << "Array Position -->" << Iter - Vector.begin() << std::endl; else std::cout << "not found" << std::endl; } | cs |
내용)
search()함수는 파라메터에 따라서 다른 기능을 합니다.
- search(w,x,y,z)(17줄)
w부터 x까지의 값들 중에서 y부터 z사이의 값들이 있는지 (순서고려) 확인합니다. 만약 없다면 end()를 리턴하며 있다면 같은 값들 중에서 제일 앞에 있는 값을 가리키는 iterator를 리턴합니다. - search(v,w,x,y,z)(25줄)
v부터 w까지의 값들 중에서 z에 부합하는 x부터 y사이의 값들이 있는지 (순서고려) 확인합니다. 만약 없다면 end()를 리턴하며 있다면 같은 값들 중에서 제일 앞에 있는 값을 가리키는 iterator를 리턴합니다.
결과)