How to set needle type in std::binary_search

paulm

I have the following simple program that will binary_search for an item:

#include <vector>
#include <algorithm>
#include <string>
#include <iostream>

class Record
{
public:
    Record() = default;
    Record(std::string name, int data) : mName(name), mData(data) { }
    std::string mName;
    int mData = 0;
};

int main(int, char**)
{
    std::vector<Record> recs;

    recs.emplace_back(Record("1", 1));
    recs.emplace_back(Record("55555", 2));
    recs.emplace_back(Record("333", 3));
    recs.emplace_back(Record("qwertyuiop", 4));
    recs.emplace_back(Record("22", 5));
    recs.emplace_back(Record("4444", 6));

    std::cout << "Unsorted:" << std::endl;
    for (auto& rec : recs)
    {
        std::cout << "Name: "  << rec.mName << " Data: " << rec.mData << std::endl;
    }
    std::cout << std::endl;

    std::stable_sort(recs.begin(), recs.end(), [](const Record& lhs, const Record& rhs) -> bool
    {
        return lhs.mName.length() < rhs.mName.length();
    });

    std::cout << "Sorted:" << std::endl;
    for (auto& rec : recs)
    {
        std::cout << "Name: " << rec.mName << " Data: " << rec.mData << std::endl;
    }
    std::cout << std::endl;

    if (std::binary_search(
        recs.begin(), 
        recs.end(),
        Record("qwertyuiop", 4),
        [](const Record& lhs, const Record& rhs) -> bool 
    {
        return lhs.mName < rhs.mName;
    }))
    {
        std::cout << "Found" << std::endl;
    }
    else
    {
        std::cout << "Not found" << std::endl;
    }


    return 0;
}

How can I search the vector of Records based on another type of T? For example std::string instead of Record?

Something like:

 if (std::binary_search(
            recs.begin(), 
            recs.end(),
            "qwertyuiop",
            [](const Record& lhs, const std::string& rhs) -> bool 
        {
            return lhs.mName < rhs.mName;
        }))
        {
            std::cout << "Found" << std::endl;
        }
        else
        {
            std::cout << "Not found" << std::endl;
        }

Is what I want - basically I don't want to construct a Record to search for it as this presents a performance issue for me.

Benjamin Lindley

You can create a function object type that handles heterogeneous comparisons:

struct comp_t
{
    bool operator()(Record const& lhs, std::string const& rhs) const {
        return lhs.mName < rhs;
    }

    bool operator()(std::string const& lhs, Record const& rhs) const {
        return lhs < rhs.mName;
    }
};

Then you can call binary_search like this:

std::binary_search(recs.begin(), recs.end(),
                   std::string("qwertyuiop"),
                   comp_t{})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set needle type in std::binary_search

From Dev

How to sort an array of structs with std::binary_search or std::sort

From Dev

How to disable mail search in tracker-needle?

From Dev

Why does std::binary_search use ForwardIterator, not RandomIterator?

From Dev

Why does std::binary_search return bool?

From Dev

Why does std::binary_search use ForwardIterator, not RandomIterator?

From Java

How set std::string type on lldb?

From Dev

How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

From Dev

Why is this implemented binary search so much slower than std::binary_search()?

From Dev

How to use std::conditional to set type depending on template parameter type

From Dev

Binary_search in STL set over set's member function find?

From Dev

Binary_search in STL set over set's member function find?

From Dev

How I remove the needle in string

From Dev

How do I search a PHP array where the needle doesn't exactly match a value?

From Dev

How can I search an std::map using a key of a different type

From Dev

Replacing std::map with std::set and search by index

From Dev

Replacing std::map with std::set and search by index

From Dev

How can I use std::set_intersection for 2 distinct but related types and output into another type

From Dev

How can I use std::set_intersection for 2 distinct but related types and output into another type

From Dev

Optimal way to search a std::set

From Dev

Optimal way to search a std::set

From Dev

How to get String after needle in PHP?

From Dev

How to get String after needle in PHP?

From Dev

How to set content type?

From Dev

How to set "search_type" to "count" in elasticsearch-rails?

From Dev

How to set "search_type" in elasticsearch-js?

From Dev

Binary_Search on vector of pars

From Dev

How can I do a binary search on a part of the keys in a std::map?

From Dev

Binary search in std::vector

Related Related

  1. 1

    How to set needle type in std::binary_search

  2. 2

    How to sort an array of structs with std::binary_search or std::sort

  3. 3

    How to disable mail search in tracker-needle?

  4. 4

    Why does std::binary_search use ForwardIterator, not RandomIterator?

  5. 5

    Why does std::binary_search return bool?

  6. 6

    Why does std::binary_search use ForwardIterator, not RandomIterator?

  7. 7

    How set std::string type on lldb?

  8. 8

    How is C++'s std::set class able to implement a binary tree for ANY type of data structure?

  9. 9

    Why is this implemented binary search so much slower than std::binary_search()?

  10. 10

    How to use std::conditional to set type depending on template parameter type

  11. 11

    Binary_search in STL set over set's member function find?

  12. 12

    Binary_search in STL set over set's member function find?

  13. 13

    How I remove the needle in string

  14. 14

    How do I search a PHP array where the needle doesn't exactly match a value?

  15. 15

    How can I search an std::map using a key of a different type

  16. 16

    Replacing std::map with std::set and search by index

  17. 17

    Replacing std::map with std::set and search by index

  18. 18

    How can I use std::set_intersection for 2 distinct but related types and output into another type

  19. 19

    How can I use std::set_intersection for 2 distinct but related types and output into another type

  20. 20

    Optimal way to search a std::set

  21. 21

    Optimal way to search a std::set

  22. 22

    How to get String after needle in PHP?

  23. 23

    How to get String after needle in PHP?

  24. 24

    How to set content type?

  25. 25

    How to set "search_type" to "count" in elasticsearch-rails?

  26. 26

    How to set "search_type" in elasticsearch-js?

  27. 27

    Binary_Search on vector of pars

  28. 28

    How can I do a binary search on a part of the keys in a std::map?

  29. 29

    Binary search in std::vector

HotTag

Archive