C++ A Collection of Objects - c++

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. :)

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.

Related

Is there a purpose for tuples besides being immutable?

I frequently see the use of tuples, in contrast to lists, which are mutable. As an intermediate programmer in high school, I am still confused as to the usefulness of tuples.
Are there any other reasons to use them besides their immutability (which is the major response I see to this question)? I know lists can be changed, but is that really such a big problem when programming, other than 'accidents' that might change a list you don't want changed?
Another reason I see, like in this post -- https://stackoverflow.com/questions/30518013/is-there-a-difference-between-a-list-and-a-tuple -- is that you can imagine them different conceptually, such as by imagining them as coordinates, rather than an array of numbers. But this isn't really a function, but a practice that arose because of the freedom for an unchanging structure that tuples supposedly give.
What I'm asking is there a need to have defined a totally new data structure that doesn't really serve a new purpose in my opinion. Thanks! hope this question isn't too dumb :\
Tuples are immutable
Tuples can describe things like coordinates and dimensionality better than list
To guard against accidents
Tuples can be put into set and used as keys for dict

Clojure list vs. vector vs. set

I'm new to Clojure. Apologies if it's a stupid question!
Should I use a set instead of a vector or list each time I don't care about the order of the items? What is the common criteria to decide between these three when order is not necessary?
It really depends on how you will be using the items.
If you will be searching for items, use a set.
If you will be processing it sequentially use a list.
If you will be chopping it into even sized chunks (like while sorting) use a vector.
If you need to count the length use a vector.
If you will be typing these by hand use a vector (to save a little quoting)
In practice most of the processing I see involves turning the data into a seq and processing that so the distinctions between list and vector are often a matter personal taste.
In general, you want a set when your primary concern is "Is this thing in this group?" Besides not preserving order, sets also only hold a given value once. So if you care about the precise placement of values, a vector is more what you want. If you mainly care about testing for membership, a set is more appropriate.
Yes, use a set. Unless you have very, very good reasons to pick something else (performance, memory use, ...) the set is the correct choice.
Remember that programming is primarily about communicating with the human reader of your code and not with the computer. By using a set you make it totally clear that the order of elements is irrelevant (and you are not expecting duplicate values) helping the reader understand your intentions and your own mental mind set.

Is using a map where value is std::shared_ptr a good design choice for having multi-indexed lists of classes?

problem is simple:
We have a class that has members a,b,c,d...
We want to be able to quickly search(key being value of one member) and update class list with new value by providing current value for a or b or c ...
I thought about having a bunch of
std::map<decltype(MyClass.a/*b,c,d*/),shared_ptr<MyClass>>.
1) Is that a good idea?
2) Is boost multi index superior to this handcrafted solution in every way?
PS SQL is out of the question for simplicity/perf reasons.
Boost MultiIndex may have a distinct disadvantage that it will attempt to keep all indices up to date after each mutation of the collection.
This may be a large performance penalty if you have a data load phase with many separate writes.
The usage patterns of Boost Multi Index may not fit with the coding style (and taste...) of the project (members). This should be a minor disadvantage, but I thought I'd mention it
As ildjarn mentioned, Boost MI doesn't support move semantics as of yet
Otherwise, I'd consider Boost MultiIndex superior in most occasions, since you'd be unlikely to reach the amount of testing it received.
You want want to consider containing all of your maps in a single class, arbitrarily deciding on one of the containers as the one that stores the "real" objects, and then just use a std::map with a mapped type of raw pointers to elements of the first std::map.
This would be a little more difficult if you ever need to make copies of those maps, however.

Initializing a AS 3.0 vector with type array - Vector.<Array>? (plus C++ equivelant)

I have a quick question for you all. I'm trying to convert over some ActionScript code to C++ and am having a difficult time with this one line:
private var edges:Vector.<Array>
What is this exactly? Is this essentially a multidimensional vector then? Or is this simply declaring the vector as a container? I understand from researching that vectors, like C++ vectors, have to be declared with a type. However, in C++ I can't just put down Array, I have to use another vector (probably) so it looks like:
vector<vector<T> example;
or possibly even
vector<int[]> example;
I don't expect you guys to know the C++ equivalent because I'm primarily posting this with AS tags, but if you could confirm my understand of the AS half, that would be great. I did some googling but didn't find any cases where someone used Array as it's type.
From Mike Chambers (adobe evangelist) :
"Essentially, the Vector class is a typed Array, and in addition to ensuring your collection is type safe, can also provide (sometimes significant) performance improvements over using an Array."
http://www.mikechambers.com/blog/2008/08/19/using-vectors-in-actionscript-3-and-flash-player-10/
Essentially the vector in C++ is based on the same principles. As far as porting a vector of Arrays in AS3 to C++, well that's not a conversion that is clear cut in principle, as you could have a collection (array) of various types in C++, such as a char array. However, it appears you've got the idea, as you've pretty much posted examples of both avenues in your question.
I would post some code but I think you've got it exactly. Weather you use a vector within a vector or you declare a specifically typed collection I think comes down to a matter of what works best for you specific project.
Also you might be interested in:
http://www.mikechambers.com/blog/2008/09/24/actioscript-3-vector-array-performance-comparison/

Use stl vectors to manage opengl buffer objects

I don't think I am the first one to think about this ... but would it be possible to write a STL allocator that manages VRAM/Buffer Objects(BO) in OpenGL?
As a result of this Question I currently use vectors to write to and read from BOs.
Additionally i use some templating to map BOs as almost anything i like. Like this:
TypedBufferObject<someKindOfStruct> tbo = getTBO();
someKindOfStruct* mapPtr = tbo.map(GL_READ_WRITE);
This works quite well ... but isn't really safe when it comes to the number of elements that map contains.
Has someone implemented a BO-based allocator for STL? And (if not) would it actually be possible?
This article by Matt Austern is the classic reference for creating your own allocator. Allocator have their nooks and crannies, but when you know them writing your own isn't really all that hard.