#List subsequences

9 messages · Page 1 of 1 (latest)

weak swallow
#

In Python there is something like this: list[1:3] that returns the subsequence of the list including the second and third element. What is its equivalence in C++?

round narwhalBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#
template<
    std::input_or_output_iterator I,
    std::sentinel_for<I> S = I,
    ranges::subrange_kind K =
        std::sized_sentinel_for<S, I>
            ? ranges::subrange_kind::sized
            : ranges::subrange_kind::unsized>
requires(
    K == ranges::subrange_kind::sized ||
    !std::
        sized_sentinel_for<S, I>) class subrange
    : public ranges::view_interface<
          subrange<I, S, K>>
Defined in
winter quail
#

But that’s kinda advanced

#

Some containers have built in functions to do this, like std::string

spiral swift
#

The equivalent in C++ is an iterator pair (a lot of functions like std::sort or constructors for std::vector take a begin and end iterator). Newer version pack iterator pairs into ranges: https://en.cppreference.com/w/cpp/ranges/range

#

It really depends on what you want to do with the sublist. std::vector sublist(std::begin(v) + 1, std::begin(v) + 3); is more or less equivalent to the Python code, but usually you want to avoid copies.

hot dagger
#

since C++20, you could pack the subrange into a std::span

round narwhalBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.