Namespaces
Variants

std::multiset<Key,Compare,Allocator>:: find

From cppreference.net

iterator find ( const Key & key ) ;
(1) (constexpr seit C++26)
const_iterator find ( const Key & key ) const ;
(2) (constexpr seit C++26)
template < class K >
iterator find ( const K & x ) ;
(3) (seit C++14)
(constexpr seit C++26)
template < class K >
const_iterator find ( const K & x ) const ;
(4) (seit C++14)
(constexpr seit C++26)
1,2) Findet ein Element mit einem Schlüssel, der äquivalent zu key ist. Wenn mehrere Elemente mit dem angeforderten Schlüssel im Container vorhanden sind, kann eines davon zurückgegeben werden.
3,4) Findet ein Element mit einem Schlüssel, der äquivalent zu x vergleicht.
Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn Compare transparent ist. Sie ermöglicht den Aufruf dieser Funktion ohne Konstruktion einer Instanz von Key .

Inhaltsverzeichnis

Parameter

key - Schlüsselwert des zu suchenden Elements
x - ein Wert beliebigen Typs, der transparent mit einem Schlüssel verglichen werden kann

Rückgabewert

Ein Iterator zum angeforderten Element. Wenn kein solches Element gefunden wird, wird ein past-the-end (siehe end() ) Iterator zurückgegeben.

Komplexität

Logarithmisch in der Größe des Containers.

Hinweise

Feature-Test Makro Wert Std Funktion
__cpp_lib_generic_associative_lookup 201304L (C++14) Heterogener Vergleichslookup in assoziativen Containern ; Überladungen ( 3,4 )

Beispiel

#include <iostream>
#include <set>
struct LightKey
{
    int x;
};
struct FatKey
{
    int x;
    int data[1000]; // a heavy blob
};
// As detailed above, the container must use std::less<> (or other transparent
// Comparator) to access these overloads. This includes standard overloads,
// such as comparison between std::string and std::string_view.
bool operator<(const FatKey& fk, const LightKey& lk) { return fk.x < lk.x; }
bool operator<(const LightKey& lk, const FatKey& fk) { return lk.x < fk.x; }
bool operator<(const FatKey& fk1, const FatKey& fk2) { return fk1.x < fk2.x; }
int main()
{
    // Simple comparison demo.
    std::multiset<int> example{1, 2, 3, 4};
    if (auto search = example.find(2); search != example.end())
        std::cout << "Found " << (*search) << '\n';
    else
        std::cout << "Not found\n";
    // Transparent comparison demo.
    std::multiset<FatKey, std::less<>> example2{{1, {}}, {2, {}}, {3, {}}, {4, {}}};
    LightKey lk = {2};
    if (auto search = example2.find(lk); search != example2.end())
        std::cout << "Found " << search->x << '\n';
    else
        std::cout << "Not found\n";
}

Ausgabe:

Found 2
Found 2

Siehe auch

gibt die Anzahl der Elemente zurück, die einem bestimmten Schlüssel entsprechen
(öffentliche Elementfunktion)
gibt den Bereich der Elemente zurück, die einem bestimmten Schlüssel entsprechen
(öffentliche Elementfunktion)