#include <iostream>
#include <vector>
#include <algorithm>
#if 0
struct Person {
int age;
std::string name;
Person(int a, std::string n) : age(a), name(n) {}
};
// Comparator to compare Persons by age
bool compareByAge(const Person &a, const Person &b) {
return a.age < b.age;
}
int main() {
std::vector<Person> people = {{30, "Alice"}, {40, "Bob"}, {50, "Charlie"}};
Person target(40, "Target");
// Find the lower bound using the custom comparator
auto it = std::lower_bound(people.begin(), people.end(), target, compareByAge);
if (it != people.end()) {
std::cout << "Lower bound person is " << it->name << " with age " << it->age << "\n";
} else {
std::cout << "Person not found in the vector.\n";
}
return 0;
}
int main() {
std::vector<int> vec = {1, 3, 4, 6, 8, 9};
int value = 5;
// Find the lower bound
auto it = std::lower_bound(vec.begin(), vec.end(), value);
if (it != vec.end()) {
std::cout << "Lower bound of " << value << " is at index " << (it - vec.begin()) << "\n";
std::cout << "Value: " << *it << "\n";
} else {
std::cout << "Value not found in the vector.\n";
}
return 0;
}
#endif
int main() {
std::vector<int> vec = {1, 2, 4, 4, 5, 7, 9};
int value = 4;
// Find the lower bound
auto it = std::lower_bound(vec.begin(), vec.end(), value);
if (it != vec.end()) {
std::cout << "Lower bound of " << value << " is at index " << (it - vec.begin()) << "\n";
std::cout << "Value: " << *it << "\n";
} else {
std::cout << "Value not found in the vector.\n";
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: