Bibliothèque de cartes C ++ - fonction lower_bound ()

La description

La fonction C ++ std::map::lower_bound()renvoie un itérateur pointant vers le premier élément qui n'est pas inférieur à la clé k .

Déclaration

Voici la déclaration de l'en-tête std :: map de la fonction std :: map :: lower_bound ().

C ++ 98

iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;

Paramètres

k - Clé à rechercher.

Valeur de retour

Si l'objet est qualifié de constante, la méthode retourne un itérateur constant sinon un itérateur non constant.

Des exceptions

Cette fonction membre ne lève pas d'exception.

Complexité temporelle

Logarithmique ie O (log n)

Exemple

L'exemple suivant montre l'utilisation de la fonction std :: map :: lower_bound ().

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   auto it = m.lower_bound('b');

   cout << "Lower bound is " << it->first << 
      " = " << it->second << endl;

   return 0;
}

Compilons et exécutons le programme ci-dessus, cela produira le résultat suivant -

Lower bound is b = 2