Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ A Collection of Objects

i'm self learning C++ (using schaum's programming with c++ book), and i can't find anything in the book that explains how to create a collection of objects? For example say you have a class of books, and a class of stores, and you want to create a collection of book objects in the store class, how would this be implemented? I'm confused as with arrays you have to give a set size don't you? So what if you don't yet know the size needed? I'm assuming an array is not the best thing to use... Also sorry if the book/store class example is a bad example. :)

like image 946
binary101 Avatar asked Dec 30 '25 17:12

binary101


1 Answers

You seem to be catching on quickly. No, arrays are generally not the best thing to use.

The standard library has a number of collection classes such as vector, deque, list, set, map, etc. From the sound of things, you might want a vector or a multimap. A vector is pretty much like an array, except that it resizes as needed when you insert objects. A map gives you the ability to look things up based on a field, such as looking a book up based on the title or author.

like image 192
Jerry Coffin Avatar answered Jan 01 '26 10:01

Jerry Coffin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!