Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to find the first value greater than or equal to a search value in a map with C++

1 Answer

0 votes
#include <iostream>
#include <map>

int main() {
    std::map<int, std::string> mp = {
        {1, "one"},
        {3, "three"},
        {5, "five"},
        {7, "seven"},
        {9, "nine"}
    };

    int searchValue = 4;
    auto it = mp.lower_bound(searchValue);

    if (it != mp.end()) {
        std::cout << "First value greater than or equal to " << searchValue << " is: " 
                  << it->first << " -> " << it->second;
    } else {
        std::cout << "No value found greater than or equal to " << searchValue;
    }
}


 
/*
run:

First value greater than or equal to 4 is: 5 -> five
 
*/

 



answered Jan 20, 2025 by avibootz
...