Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL non-copying wrapper around an existing array?

Is it possible to create an STL-like container, or even just an STL-style iterator, for an existing array of POD-type elements?

For example, suppose I have an array of ints. It would be convenient to be able to call some of the STL functions, such as find_if, count_if, or sort directly on this array.

Non-solution: copying the entire array, or even just references to the elements. The goal is to be very memory- and time-saving while hopefully allowing use of other STL algorithms.

like image 560
Tyler Avatar asked Oct 06 '08 20:10

Tyler


1 Answers

You can call many of the STL algorithms directly on a regular C style array - they were designed for this to work. e.g.,:

int ary[100];
// init ...

std::sort(ary, ary+100); // sorts the array
std::find(ary, ary+100, pred); find some element

I think you'll find that most stuff works just as you would expect.

like image 146
1800 INFORMATION Avatar answered Oct 10 '22 12:10

1800 INFORMATION