BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlb_pairutil

Detailed Description

Provide support functions for bsl::pair.

Outline

Purpose

Provide support functions for bsl::pair.

Classes

Description

This component provides the class bdlb::PairUtil, which has the following tools for working with bsl::pair:

Usage

This section illustrates intended use of this component.

Example 1: Basic Usage of tie Function

Suppose we need to implement a function that takes a bsl::map and stores into out-parameters the key and value corresponding to the first entry in the map. Using bsl::maps container interface, we can obtain a reference to a bsl::pair of the key and value. We can then use bdlb::PairUtil::tie to assign from both the key and value in a single expression:

/// Load into the specified `key` and the specified `value` the key and
/// value for the first entry in the specified `map` and return `true`,
/// or else fail by storing 0 and an empty string and return `false`
/// when `map` is empty.
bool getFirst(int *key,
bsl::string *value,
{
if (map.empty()) {
*key = 0;
value->clear();
return false; // RETURN
}
bdlb::PairUtil::tie(*key, *value) = *map.begin();
return true;
}
void usageExample()
{
map[30782530] = "bbi10";
int uuid;
bsl::string username;
bool result = getFirst(&uuid, &username, map);
assert(result);
assert(30782530 == uuid);
assert("bbi10" == username);
}
Definition bslstl_string.h:1252
void clear() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_string.h:6043
Definition bslstl_map.h:653
bool empty() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_map.h:4030
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_map.h:3300
static bsl::pair< t_FIRST &, t_SECOND & > tie(t_FIRST &first, t_SECOND &second)
Definition bdlb_pairutil.h:330

Example 2: Adapting bsl Container For Ranges

Let's assume that we have a bsl::map storing employee indexes and their names, and we want to get a list of employee names:

bsl::map<int, bsl::string_view> employees{{1, "John Dow"},
{2, "Jane Dow"},
{3, "James Dow"}};

However, if we were to try and access the names using bsl::views::values we would see a compilation error:

auto names = employees | bsl::views::values; // does not compile auto namesIt = names.begin(); assert("John Dow" == *namesIt);

This fails to because bsl::pair, unlike the std::pair, does not model the tuple-like concept, which is a requirement of the bsl::views::values. This problem can be resolved using the bdlb::PairUtil::adaptForRanges function on the container:

auto names = employees | bdlb::PairUtil::adaptForRanges
| bsl::views::values;
auto namesIt = names.begin();
assert("John Dow" == *namesIt);

And of course this function allows you to create chains of adaptors using a pipeline operator:

const auto startsWithJa = [](bsl::string_view name) -> bool
{
return name.starts_with("Ja");
};
auto jaNames = employees | bdlb::PairUtil::adaptForRanges
| bsl::views::values
| bsl::views::filter(startsWithJa);
assert(bsl::ranges::equal(jaNames,
"James Dow"}));
Definition bslstl_stringview.h:471
Definition bslstl_vector.h:1120