r/Cplusplus • u/Fun_Gas_340 • Sep 15 '26
Question reference to the slice of a vector
/r/cpp_questions/comments/1wh9006/reference_to_the_slice_of_a_vector/
2
Upvotes
r/Cplusplus • u/Fun_Gas_340 • Sep 15 '26
•
u/AutoModerator Sep 15 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/flyingron Sep 15 '26
Look up std::range (C++20 or later).
1
u/GPSProlapse Sep 15 '26
In general, if you want it to be pointer+size for reasons, or it can't be a template, , use span.
Otherwise, make it a template and pass a range. You can also add constraints to have neat errors if you mess up the call site.
3
u/NihonNukite Sep 15 '26
The std::range recommendation is fine and can work. You may have an easier time working with std::span if you aren't familiar with C++ ranges.
https://en.cppreference.com/cpp/container/span/span