All Ordered Bound Operations

All Ordered Bound Operations

A compact scan page for lower_bound, upper_bound, equal_range, and related ordered-lookup operations across sorted ranges and ordered containers.

How to use this reference page

Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.

  • Scan the top of the page first to identify the primary types, functions, or algorithm families involved.
  • Use the nearby-page links when your question is really about a companion header, related algorithm family, or broader subsystem.
  • Validate tricky behavior with a small compileable example before relying on memory for details like invalidation, ordering, allocation, or lifetime rules.

All Ordered Bound Operations

Core bound operations

Where they appear

Mental model

Practical rules

Small worked example

#include <algorithm>
#include <vector>

int main() {
	std::vector<int> values{1, 3, 3, 5, 8};
	auto first = std::lower_bound(values.begin(), values.end(), 3);
	auto last = std::upper_bound(values.begin(), values.end(), 3);
	return static_cast<int>(last - first);
}

The result is the count of matching 3 values. That is why bound operations are often more useful than a plain boolean membership check: they tell you where a key belongs and how wide the matching range is.

Selection guide

Example in practice

#include <map>
#include <string>

int main() {
    std::map<std::string, int> counts{std::pair{"Ada", 1}, std::pair{"Bjarne", 2}};
    auto [it, inserted] = counts.insert({"Grace", 3});
    return inserted ? it->second : 1;
}