Skip to main content
Commonmark migration
Source Link

#C++14, 87 85 bytes

C++14, 87 85 bytes

#include<algorithm>
[](int i,auto v){return std::binary_search(begin(v),end(v),i)-1;}

Assumes a sorted data structure which has iterators as input.

Example:

#include<algorithm>
#include<iostream>

auto f = [](int i, auto v){ return std::binary_search(begin(v), end(v), i) - 1; };

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    
    std::cout << f(3, v);
}

#C++14, 87 85 bytes

#include<algorithm>
[](int i,auto v){return std::binary_search(begin(v),end(v),i)-1;}

Assumes a sorted data structure which has iterators as input.

Example:

#include<algorithm>
#include<iostream>

auto f = [](int i, auto v){ return std::binary_search(begin(v), end(v), i) - 1; };

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    
    std::cout << f(3, v);
}

C++14, 87 85 bytes

#include<algorithm>
[](int i,auto v){return std::binary_search(begin(v),end(v),i)-1;}

Assumes a sorted data structure which has iterators as input.

Example:

#include<algorithm>
#include<iostream>

auto f = [](int i, auto v){ return std::binary_search(begin(v), end(v), i) - 1; };

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    
    std::cout << f(3, v);
}
Source Link
sweerpotato
  • 2.5k
  • 1
  • 19
  • 33

#C++14, 87 85 bytes

#include<algorithm>
[](int i,auto v){return std::binary_search(begin(v),end(v),i)-1;}

Assumes a sorted data structure which has iterators as input.

Example:

#include<algorithm>
#include<iostream>

auto f = [](int i, auto v){ return std::binary_search(begin(v), end(v), i) - 1; };

int main()
{
    std::vector<int> v = { 1, 2, 3, 4, 5 };
    
    std::cout << f(3, v);
}