C++ STL
search()
parkit
2019. 5. 20. 13:52
728x90
반응형
http://www.cplusplus.com/reference/algorithm/search/
a 벡터와 b 벡터가 있으면,
a 벡터 안에서 b 벡터가 있는지의 확인할 수 있다.
(시작 인덱스 반환 가능)
a 벡터에 없는 원소가 b 벡터에 있으면 실패.
따라서, a 벡터가 더 길어야 한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <bits/stdc++.h> using namespace std; int main(void) { vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; vector<int> c = { 12, 23, 43, 5, 6, 7, 12 }; vector<int> d = { 7, 8, 9 }; auto itr1 = search(v.begin(), v.end(), c.begin(), c.end()); auto itr2 = search(v.begin(), v.end(), d.begin(), d.end()); if (itr1 != v.end()) cout << itr1 - v.begin() << '\n'; else printf("못 찾음.\n"); if (itr2 != v.end()) cout << itr2 - v.begin() << '\n'; else printf("못 찾음.\n"); return 0; } | cs |
728x90
반응형