Vectors and Pointers
A vector is a container, you can put pointers in vectors
Pointers are stored contiguously but the memory it points to is on the heap, allocated by default
The memory that is pointed to is thus not guaranteed to be contiguous
If a vector pointer points to some information that was manually allocated, it will have to be manually deallocated too
When you don't want to share copies of objects, arrays or pointers are good
If you do copy, a change will only affect one and it will cause slowdown
Push-back is used to add memory to the end of a container
Must deallocate the pointers before clearing the vector
Unique pointers, however, will deallocate themselves, but any information can only have 1 unique pointer pointing to it
level3.push-back(RoomUniPointer((new _____)) example unique pointer declaration syntax
Using shared pointers, use std::make_shared(room x)
Shared pointers guarantee that the pointers will stay valid even if the container is cleared
Can use a weak pointer to make a cache that can't be cleared
Weak pointers must be connected to be used
Associative containers are related to what is in them
For example, a container of strings would then have the properties of a string
A container of strings could be sorted alphabetically
It can also be searched by name, as opposed to being searched by its number
Until now, a container's order was only determined by what order the items were added
A set is like a container but it can only hold unique 'keys'
Sets are also in a specific order
The value of the key also determines how unique ?
Sorting in a set uses a less-than comparison (lowest first)
Sorting in a vector is slower, but it still works
Vector sorting has to go through every item to make sure it is in the right place, and this must be done every time a new item is added too
Iteration is faster in a vector though as it is simply added to the end
Sets need to know only what is to the left and right of the current item, making for faster sorting
Sets also do not have to shift all items left or right when an item is added or removed
In a set, the value is the key, which is also the element
Find:set is a logarithmic search, whereas find:std is a linear search
find_if requires a boolean predicate; a yes or no state
Associative containers cannot do this
In a map, the lookup key is separate from the value stores in the array
Pair - look up first, return second. A string is usually the key
This has the same overheard as a set
Multi-sets can have duplicates, but they are the same as a set otherwise
Same for multi-maps
Useful when caching where hash might be duplicated
No real reason to use...
No comments:
Post a Comment