Answers for "search array c++"

C++
0

C++ array find method

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;

int main() {
    array<int, 5> arrayfind{ 3,4,1,2,5 };
    int Finding = 2;//an element to find
    bool Founded;

    Founded = binary_search(arrayfind.begin(), arrayfind.end(), Finding);
    //if the element that you're looking for is in the array,
    //bool Founded will be true
    if (Founded == true) {
        cout << Finding << " Is in the array!";
    }
    else {
        cout << Finding << " Is not in the array!";
    }
}
Posted by: Guest on August-09-2021
0

array search c++

#include <iostream>
2 #include <array>
3 #include <string>
4 #include <algorithm>
5
6 using namespace std;
7
8 int main(){
9 array<string, 4> colours = {"blue", "black", "red", "green"};
10 sort(colours.begin(), colours.end()); //must be sorted
11 string key = "black";
12 //look for black
13 bool found = binary_search(colours.begin(), colours.end(), key);
14 if (found){
15 cout << "We found the key 'black'" << endl;
16 }
17 return 0;
18 }
Posted by: Guest on May-25-2020

Browse Popular Code Answers by Language