C++/Vector/vector bound

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Use lower_bound on vector

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <algorithm>
  4. include <vector>

using namespace std; int main() {

 vector<int> v(5);
 bool found;
 v[1] = 7; v[2] = 7; v[3] = 7; v[4] = 8;
 vector<int>::iterator k;
 k = lower_bound(v.begin(), v.end(), 7);
 cout << *k;
 return 0;

} /* 7

*/        
 </source>


Use upper_bound on vector

<source lang="cpp">

  1. include <iostream>
  2. include <cassert>
  3. include <algorithm>
  4. include <vector>

using namespace std; int main() {

 vector<int> v(5);
 bool found;
 v[1] = 7; v[2] = 7; v[3] = 7; v[4] = 8;
 vector<int>::iterator k;
 k = upper_bound(v.begin(), v.end(), 7);
 cout << *k;
 return 0;

} /* 8

*/        
 </source>