Vector에 주어진 요소가 포함되어 있는지 C++에서 확인
C++에서 Vector가 주어지면 지정된 요소가 포함되어 있는지 확인하십시오.
Vector에서 요소를 검색하는 것은 Vector가 정렬되지 않는 한 선형 시간 연산입니다. 그만큼 <algorithm>
헤더는 검색에 사용할 수 있는 많은 기능을 제공합니다.
1. 사용 std::count
기능
가장 간단한 솔루션은 지정된 값을 갖는 Vector의 총 요소 수를 계산하는 것입니다. 개수가 0이 아니면 요소를 찾은 것입니다. 이것은 다음을 사용하여 쉽게 수행할 수 있습니다. std::count
기능.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::count(v.begin(), v.end(), key)) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
2. 사용 std::find
기능
효율적인 솔루션은 다음을 사용하는 것입니다. std::find
주어진 범위에서 값을 찾는 알고리즘. 이것은 보다 빠르게 수행됩니다. std::count
~부터 count()
요소 수를 얻기 위해 전체 컨테이너를 탐색하는 동안 find()
일치하는 요소가 발견되는 즉시 검색을 중지합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::find(v.begin(), v.end(), key) != v.end()) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
3. 사용 std::find_if
기능
우리는 또한 사용할 수 있습니다 std::find_if
술어가 필요한 알고리즘. 검색이 특정 조건을 충족해야 하는 경우 권장되는 방법입니다. 예를 들어, 정수 Vector에서 첫 번째 소수를 찾거나 문자열 Vector에서 특정 문자로 끝나는 첫 번째 문자열을 찾도록 요청받습니다.
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 |
#include <iostream> #include <vector> #include <algorithm> struct compare { int key; compare(int const &i): key(i) {} bool operator()(int const &i) { return (i == key); } }; int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::find_if(v.begin(), v.end(), compare(key)) != v.end()) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
4. 사용 std::any_of
기능
이것은 다음과 유사하게 작동합니다. std::find_if
알고리즘. 조건자를 만족하는 시퀀스의 첫 번째 요소를 가리키는 반복자를 반환하는 대신 조건자가 요소 중 하나에 대해 true를 반환하고 그렇지 않으면 false를 반환하면 부울 값 true를 반환합니다.
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 |
#include <iostream> #include <vector> #include <algorithm> struct compare { int key; compare(int const &i): key(i) {} bool operator()(int const &i) { return (i == key); } }; int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (std::any_of(v.begin(), v.end(), compare(key))) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
5. 사용 std::none_of
기능
그만큼 std::none_of
알고리즘은 정반대입니다. std::any_of
즉, 조건자가 주어진 범위의 모든 요소에 대해 false를 반환하면 true를 반환하고 적어도 하나의 요소에 대해 조건이 true를 반환하면 false를 반환합니다.
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 |
#include <iostream> #include <vector> #include <algorithm> struct compare { int key; compare(int const &i): key(i) {} bool operator()(int const &i) { return (i == key); } }; int main() { std::vector<int> v = { 4, 7, 5, 2, 6, 9 }; int key = 6; if (!std::none_of(v.begin(), v.end(), compare(key))) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
6. 사용 std::binary_search
기능
마지막으로 Vector가 정렬된 경우 다음을 사용하는 것이 좋습니다. std::binary_search
요소가 주어진 범위에서 발견되면 true를 반환하고 그렇지 않으면 false를 반환하는 알고리즘입니다. 이진 검색 절차 실행 O(log(n)) 시각.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 1, 2, 3, 4, 5, 6, 7 }; int key = 6; if (std::binary_search(v.begin(), v.end(), key)) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; } |
C++에서 Vector에 주어진 요소가 포함되어 있는지 여부를 결정하는 것이 전부입니다.