beautypg.com

HP Integrity NonStop H-Series User Manual

Page 189

background image

// illustrate the use of the binary search algorithm
{
// make an ordered vector of 15 random integers
vector aVec(15);
generate (aVec.begin(), aVec.end(), randomValue);
sort (aVec.begin(), aVec.end());

// see if it contains an eleven
if (binary_search (aVec.begin(), aVec.end(), 11))
cout << "contains an 11" << endl;
else
cout << "does not contain an 11" << endl;

// insert an 11 and a 14
vector::iterator where;
where = lower_bound (aVec.begin(), aVec.end(), 11);
aVec.insert (where, 11);

where = upper_bound (aVec.begin(), aVec.end(), 14);
aVec.insert (where, 14);

}

This manual is related to the following products: