# STL alternative to OwnedArray?

**URL:** <https://forum.juce.com/t/stl-alternative-to-ownedarray/29069>\
**Category:** General JUCE discussion\
**Created:** [August 21, 2018, 2:40pm UTC](https://forum.juce.com/t/stl-alternative-to-ownedarray/29069 "2018-08-21T14:40:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [August 21, 2018, 2:40pm UTC](https://forum.juce.com/t/stl-alternative-to-ownedarray/29069/1 "2018-08-21T14:40:35Z")

</div>

Is `OwnedArray` still the best option for holding an array of `Component`’s or is there an STL class that will work just as well (or even better)?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [August 21, 2018, 2:43pm UTC](https://forum.juce.com/t/stl-alternative-to-ownedarray/29069/2 "2018-08-21T14:43:46Z")

</div>

A replacement is

```auto
std::vector<std::unique_ptr<Component>> myComponents;

```

but OwnedArray is one of the classes I hold on to, because it reads much nicer…

There are technical opinions which ones to choose out there for sure, I am looking forward to hear them…

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [August 21, 2018, 2:59pm UTC](https://forum.juce.com/t/stl-alternative-to-ownedarray/29069/3 "2018-08-21T14:59:22Z")

</div>

> [@daniel](#):
>
> A replacement is
> 
> ```auto
> std::vector<std::unique_ptr<Component>> myComponents;
> 
> ```

That’s a nice idea - hadn’t thought of that.

I guess the appeal of `OwnedArray` is the simplicity of adding new items (`myOwnedArray.add(new MyObjectClass())`). Sadly the equivalent using `std::vector::push_back` with the same `new` syntax doesn’t work (you’d instead have to use `std::make_unique`?) so your code starts becoming a little less pretty.

I think I’ll stick with `OwnedArray` for now until someone can convince me of a technical reason not to…

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [August 21, 2018, 3:09pm UTC](https://forum.juce.com/t/stl-alternative-to-ownedarray/29069/4 "2018-08-21T15:09:04Z")

</div>

> [@Im\_Jimmi](#):
>
> Sadly the equivalent using `std::vector::push_back` with the same `new` syntax doesn’t work

I think `std::vector<>::emplace_back` should work for this…

---

<div class="post-metadata">

**Author:** ![ImJimmi](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imjimmi/32/10843_2.png) [@ImJimmi](https://forum.juce.com/u/ImJimmi)\
**Post date:** [August 21, 2018, 3:13pm UTC](https://forum.juce.com/t/stl-alternative-to-ownedarray/29069/5 "2018-08-21T15:13:36Z")

</div>

Oh cool, I didn’t know about `emplace_back` - thanks!
