Wykorzystanie lower_bound i upper_bound do znalezienia pierwszego i ostatniego miejsca występowania poszukwanego elementu w zestawie danych.
-
#include <iostream>
-
#include <algorithm>
-
#include <vector>
-
using namespace std;
-
-
int main ()
-
{
-
int T[] = {5,1,2,3,3,2,1,1,5,5,3,4,2,9,9};
-
int n=sizeof(T)/sizeof(int); //obliczamy ilosc el. w tabeli
-
vector <int> V (T,T+n); //umieszczamy dane z tabeli w wektorze
-
-
sort (V.begin(), V.end()); //sortujemy elementy w wektorze
-
-
cout<<"\n"; //pomoc - przegladamy zawartosc wektora
-
for (int i=0 ; i<V.size(); i++)
-
cout<<V[i]<<" ";
-
-
int find;
-
find=5; //poszukiwany element
-
-
vector<int>::iterator first,last;
-
first=lower_bound (V.begin(), V.end(), find);
-
last=upper_bound (V.begin(), V.end(), find);
-
-
cout << "\n" << "Pierwsze wysytapienie: " << (first - V.begin()) ;
-
cout << "\n" << "Pierwszy element (wiekszy) nie spelniajacy zapytania: " << (last - V.begin()) ;
-
}