Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

slice container with O(1) constructor from iterable begin and end

I have a forward iterator. I want a simple iterable container which wraps it and exposes begin() and end().
I.E.

template <typename C>
void use_container(C c) {
  std::cout << c.begin();
}
int main() {
std::vector<int> v {1,2,3,4,5,5};
auto begin_ = v.begin();
auto end_ = begin + 5;
use_container(/*create a container using the `begin_` and `end_` variables*/); 
return 0;
}

Is there such a std wrapper?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

C++20 added this with std::ranges::subrange. For example the following will call use_container with a range that is a view over part of v:

use_container(std::ranges::subrange{begin_, end_});

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading