English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C ++ set upper_bound()A função é usada para retornar um iterador que aponta para um valor no contêiner set, que é maior que o valor passado como parâmetro.
iterator upper_bound (const value_type& val) const; //C++ 11 antes iterator upper_bound (const value_type& val); //C++ 11 depois const_iterator upper_bound (const value_type& val) const; //C++ 11 depois
val: Valor a ser pesquisado no contêiner de conjunto.
Ele retorna um iterador que aponta para um valor no contêiner set, que é maior que o valor passado como parâmetro. Se não houver tal elemento, ele retornará end().
Tamanho em logaritmo.
Sem alteração.
O contêiner é acessado (versões const e não const não podem modificar o contêiner).
O acesso simultâneo aos elementos da coleção é seguro.
Se uma exceção for lançada, não haverá nenhuma alteração no contêiner.
Vamos ver um exemplo simples para obter o limite superior de um valor dado:
#include <iostream> #include <set> using namespace std; int main(void) { set<char> m = {'a', 'b', 'c', 'd'}; auto it = m.upper_bound('b'); cout << "O limite superior de b é (>): " << *it << endl; return 0; }
Saída:
O limite superior de b é (>): c
No exemplo acima, quando tentamos encontrar o limite superior do elemento b, ele retornará o valor maior do elemento b, ou seja, c
Vamos ver um exemplo simples, para excluir elementos do set de baixo para cima:
#include <iostream> #include <set> using namespace std; int main () { set<int> myset; set<int>::iterator itlow,itup; for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itlow = myset.lower_bound(30); // ^ itup = myset.upper_bound(60); // ^ myset.erase(itlow,itup); // 10 20 70 80 90 std::cout << "myset contains:"; for (set<int>::iterator it = myset.begin(); it != myset.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; }
Saída:
myset contains: 10 20 70 80 90
In the above example, the erase() function will remove the elements of the set from the lower bound (=) to the upper bound (>), and print the rest.
Let's see a simple example:
#include<iostream> #include<set> using namespace std; int main() { // Initialize the container set<int> mp; // Insert elements in random order mp.insert( 12 ); mp.insert( 11 ); mp.insert( 15 ); mp.insert( 14 ); // When11Exists auto it = mp.upper_bound(11); cout << "key11The upper limit is "; cout << (*it << endl; // When13Does not exist it = mp.upper_bound(13); cout << "key13The upper limit is "; cout << (*it << endl; // When17When greater than the maximum key value, sorted by size // The return value of mp as key, value 0. it = mp.upper_bound(17); cout << "key17The upper limit is "; cout << (*it); return 0; }
Saída:
key11The upper limit is 12 key13The upper limit is 14 key17The upper limit is 4
In the above example, when trying to find the upper limit of a value that does not exist in the set container but does not exceed the maximum value, it will return a larger value, that is, when trying to find13The upper limit will return14When trying to find the upper limit of a value that does not exist in the set and exceeds the maximum value of the container, it will return to end().
Let's see a simple example:
#include <set> #include <iostream> int main( { using namespace std; set<int> s1; set<int> :: const_iterator s1_AcIter, s1_RcIter; s1.insert( 10 ); s1.insert( 20 ); s1.insert( 30 ); s1_RcIter = s1.upper_bound( 20 ); cout << "set s1A chave é maior que2The first element of 0 is: " << *s1_RcIter << "." << endl; s1_RcIter = s1.upper_bound( 30 ); // If no matching key is found, then return end() if ( s1_RcIter == s1.end() ) cout << "set s1Não há chave valor maior que o elemento 30. << endl; else cout << "key> 4The set s of 01The element is: " << *s1_RcIter << "." << endl; //You can find the element at a specific position in the set //Endereçamento de posição usando iteradores de desreferência s1_AcIter = s1.begin(); s1_RcIter = s1.upper_bound( *s1_AcIter ); cout << "s1O primeiro elemento é: " << endl << "s1O elemento inicial é: " << *s1_RcIter << "." << endl; return 0; }
Saída:
Conjunto s1A chave é maior que2O primeiro elemento é: 30. Conjunto s1Não há chave valor maior que o elemento 30. s1A chave do primeiro elemento é maior que s1O elemento inicial é: 20.