Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate over boost::shared_array

Tags:

c++

boost

c++98

How would you iterate over the items in a boost::shared_array? Would you do a get() on it and use a raw pointer as the iterator?

like image 983
Baz Avatar asked Jun 24 '26 08:06

Baz


1 Answers

Since you're already using boost, maybe like this:

#include <boost/shared_array.hpp>
#include <boost/range.hpp>
#include <iostream>

int main()
{
    boost::shared_array<int> arr(new int[10]());

    int* ptr = arr.get();
    for (int i : boost::make_iterator_range(ptr, ptr+10))
    {
        std::cout << i << ',';
    }
}

In any case, you need to do your own bookeeping of array's size.

like image 179
jrok Avatar answered Jun 26 '26 22:06

jrok