C++ STL, algorithm upper_bound() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the upper_bound() to find the position of the first element in an ordered range that has a value that is greater than a specified value, where the ordering criterion may be specified by a binary predicate

To show: How to use the C++ algorithm, upper_bound() member to find the position of the first element in an ordered range that has a value that is greater than a specified value, where the ordering criterion may be specified by a binary predicate in C++ programming

 

// C++ STL, algorithm upper_bound()

#include <vector>

#include <algorithm>

// for greater<int>()

#include <functional>

#include <iostream>

using namespace std;

 

// return whether modulus of elem1 is less than modulus of elem2

bool mod_lesser(int elem1, int elem2)

{

if(elem1 < 0)

elem1 = - elem1;

if(elem2 < 0)

elem2 = - elem2;

return (elem1 < elem2);

}

 

int main(void)

{

// vector container

vector <int> vec1;

// vector iterators

vector <int>::iterator Iter1, Result1;

 

// constructing vectors vec1a & vec1b with default less-than ordering

for(int i = -2; i <= 4; i++)

vec1.push_back(i);

for(int j = -3; j <= 0; j++)

vec1.push_back(j);

sort(vec1.begin(), vec1.end());

 

// print the data

cout<<"Original vector vec1 data with range sorted by the binary predicate less than is: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

// constructing vectors vec2 with range sorted by greater

vector <int> vec2(vec1);

vector <int>::iterator Iter2, Result2;

sort(vec2.begin(), vec2.end(), greater<int>());

cout<<"\nOriginal vector vec2 data with range sorted by the binary predicate greater than is: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// constructing vectors vec3 with range sorted by mod_lesser

vector <int>vec3(vec1);

vector <int>::iterator Iter3, Result3;

sort(vec3.begin(), vec3.end(), mod_lesser);

cout<<"\nOriginal vector vec3 data with range sorted by the binary predicate mod_lesser is: ";

for(Iter3 = vec3.begin(); Iter3 != vec3.end(); Iter3++)

cout<<*Iter3<<" ";

cout<<endl;

// upper_bound of -3 in vec1 with default binary predicate less <int>()

Result1 = upper_bound(vec1.begin(), vec1.end(), -3);

cout<<"\nThe upper_bound in vec1 for the element with a value of -3 is: "<<*Result1<<endl;

// upper_bound of 2 in vec2 with the binary predicate greater <int>()

Result2 = upper_bound(vec2.begin(), vec2.end(), 2, greater<int>());

cout<<"\nThe upper_bound in vec2 for the element with a value of 2 is: "<<*Result2<<endl;

// upper_bound of 3 in vec3 with the binary predicate mod_lesser

Result3 = upper_bound(vec3.begin(), vec3.end(), 3, mod_lesser);

cout<<"\nThe upper_bound in vec3 for the element with a value of 3 is: "<<*Result3<<endl;

return 0;

}

 

Output examples:

 

Original vector vec1 data with range sorted by the binary predicate less than is: -3 -2 -2 -1 -1 0 0 1 2 3 4

Original vector vec2 data with range sorted by the binary predicate greater than is: 4 3 2 1 0 0 -1 -1 -2 -2 -3

Original vector vec3 data with range sorted by the binary predicate mod_lesser is: 0 0 -1 -1 1 -2 -2 2 -3 3 4

The upper_bound in vec1 for the element with a value of -3 is: -2

The upper_bound in vec2 for the element with a value of 2 is: 1

The upper_bound in vec3 for the element with a value of 3 is: 4

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index