Provide a standard compatible equality predicate for C-strings.
More...
Namespaces |
namespace | bdlb |
Detailed Description
- Outline
-
-
- Purpose:
- Provide a standard compatible equality predicate for C-strings.
-
- Classes:
-
- See also:
-
- Description:
- This component provides a
struct
, bdlb::CStringEqualTo
, that defines a functor that checks two null-terminated strings for equality using a case-sensitive string comparison, rather than simply comparing the two addresses (as the std::equal_to
functor would do). This comparison functor is suitable for supporting C-strings as keys in unordered associative containers. Note that the container behavior would be undefined if the strings referenced by such pointers were to change value.
-
- Usage:
- This section illustrates intended use of this component.
-
- Example 1: Basic Use of bdlb::CStringEqualTo:
- The following snippets of code illustrate how to create and use a
bdlb::CStringEqualTo
object as a binary predicate for the standard library function bsl::equal
to test that two ranges of null-terminated character strings are equal.
- First, we create few sequences with null-terminated character strings, making sure that their elements have different memory addresses:
const char hello1[] = { 'h', 'e', 'l', 'l', 'o', 0};
const char hello2[] = { 'h', 'e', 'l', 'l', 'o', 0};
const char* arrayA[3] = { "A", "B", hello1 };
const char* arrayB[3] = { "A", "B", hello2 };
Now, use bdlb::CStringEqualTo() as a binary predicate to compare sequences: Finally, we observe that bdlb::CStringEqualTo
compares character string by their values, while the default comparator compares addresses: assert( true == bdlbEqualTo );
assert( false == bslEqualTo );