C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Addition of std::contains to Simplify Container Value Checks

From: Robert Sitton <robert.sitton_at_[hidden]>
Date: Sat, 23 Dec 2023 14:35:14 -0600
Thank you for your prompt response and valuable information, and I
appreciate your input. I want to clarify the motivation behind the proposed
std::contains function and its potential benefits.

I understand that C++20 has introduced std::ranges::contains. However, my
proposal for std::contains aims to create a standardized way of checking
for the presence of a particular value in various container types. This
common interface can significantly improve code consistency and simplify
the logic for dealing with different container types.

Recently, I came across a fascinating scenario where I had to generate a
template function that could effortlessly function with a wide range of
container types such as vectors, maps, sets, and even std::pair<>. This was
while I was parsing data that was stored in a CSV. In this particular
context, I could experiment with and modify containers and search for
specific values across multiple container types without any difficulty.

Adding such a feature to the C++ standard library would be highly valuable
as it aligns with the code simplicity and consistency principles highly
valued in the C++ community.

Thanks again,

Robert


On Sat, Dec 23, 2023 at 3:51 AM ஜெய்கணேஷ் குமரன் via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> There is already one in C++20: std::ranges::contains (
> https://en.cppreference.com/w/cpp/algorithm/ranges/contains).
>
> On 23-Dec-2023, at 14:50, Robert Sitton via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
> Dear std-proposals community,
>
> I hope this email finds you well. I am writing to introduce a proposal for
> the addition of a new utility function, std::contains, to the C++ standard
> library. This function aims to streamline the common task of checking
> whether a container contains a specific element and enhance the overall
> readability of C++ code.
>
> Motivation:
> One of the frequent operations in C++ programming involves checking
> whether a container (e.g., std::vector, std::list, std::set) contains a
> particular value. While C++ provides mechanisms to achieve this, such as
> custom loops or std::find, these approaches may not always be as intuitive
> and efficient as desired.
>
> Proposal Overview:
> The proposed std::contains function is designed to simplify this operation
> and make code more expressive. It offers a straightforward and concise way
> to check if a container contains a specified element, reducing the need for
> manual iterations and enhancing code clarity.
>
> Function Signature:
> template <typename Container, typename T>
> bool std::contains(const Container& container, const T& value);
>
> Example Usage:
> std::vector<int> numbers = {1, 2, 3, 4, 5};
> bool containsThree = std::contains(numbers, 3); // true
> bool containsTen = std::contains(numbers, 10); // false
> bool containsOne = std::contains(numbers, {1, 4, 5}); // true
> bool containsTwo = std::contains(numbers, {7, 8}); // false
>
> Your Feedback:
> I would greatly appreciate your feedback and insights on this proposal.
> Before formally submitting it to the C++ committee, I believe it is
> essential to gather input from the community to ensure that the proposal
> aligns with the needs and expectations of C++ developers.
>
> Please feel free to share your thoughts, suggestions, or concerns
> regarding the proposed std::contains function. Your expertise and feedback
> are invaluable in refining and improving this idea. Thank you for your time
> and attention. I look forward to engaging in constructive discussions and
> working together to enhance the C++ standard library.
>
> Sincerely,
>
> Robert
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-12-23 20:33:41