I have a vector of integer scores = 10, 23, 29, 77, 8, 43, 56, 3, a number predict = 42, and a start index = 2 as well as a number predict = 42.

I'm looking for two values that are (greater than and less than) 42 in the range of start index = 2 and finish index = 7.

scores = 10, 23, 29, 77, 8, 43, 56, 3 (search only inside i=2 to i=7)

As a result, 29 is little less than predict=42.

Also, 43 is a touch higher than the forecasted 42.

How did I come up with these figures?

Sample Code:

```#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
vector<int> scores = {10, 23, 29, 77, 8, 43, 56, 3}; //Unsorted
int predict = 42;
int startFromIndex = 2;

int littleLessThanPredict; // = 29
int littleMoreThanPredict; // = 43

//lower_bound
//upper_bound

return 0;
}```

In consideration of this scenario,

On an unsorted range, how do I utilise the std::lower bound and std::upper bound functions?

How can I temporarily sort a portion of a vector and apply my own function?

Is there a method to get the desired result by combining the std::min element and std::lower bound functions?

Jun 15, 2022 in C++ 1,260 views

## 1 answer to this question.

What's the point of sorting your array? The complexity of the algorithm will be O(N.ln(N)). You can create an O(N) method by combining upper and lower bounds research in an unsorted array:

```#include <algorithm>
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

pair<int, int> nearestNumbers(const vector<int> &scores, int predict, int start) {
int nearestLower = INT_MIN;
int nearestUpper = INT_MAX;

for (size_t i = start; i < scores.size(); i++) {
int value = scores[i];
if (value <= predict && value > nearestLower)
nearestLower = value;
else if (value >= predict && value < nearestUpper)
nearestUpper = value;
}

return pair<int, int>(nearestLower, nearestUpper);
}

int main() {
vector<int> scores = {10, 23, 29, 77, 8, 43, 56, 3};
int predict = 42;
int start = 2;

pair<int, int> results = nearestNumbers(scores, predict, start);
cout << results.first << " " << results.second;

return 0;
}```
• 4,960 points

## How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

## What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

## How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

## How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

## Lower and Upper Bound in case of Decreasing/Non-ascending vector

Both std::lower bound and std::upper bound must have an increasing (non-decreasing) order as their objective. By giving a comparator as the 4th parameter of the functions, you may modify the meaning of "growing." To work with descending vectors, use std::greater. #include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; int main() { ...READ MORE

## C++ vector erase function not working properly

Here is the code I want to use to delete any entries with values more than 2 and fewer than 5. vector<int> myvector{3, 3, 3, 3, 3, 3, ...READ MORE

## How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

## Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE