There are two types of containers:
Sequential containers, in which the programmer controls the order in which elements are added and accessed, and the position of elements in determined when they are inserted.
The elements are organised linearly.
Associative containers, in which element are stored and linked on the basis of a key value.
The elements are not necessarily organised linearly.
Random access: Access elements in an arbitrary order with similar performance. Contrasts with sequential access where you need to go through other elements to reach the one you want.
Default: vector.
Use it unless you have a good reason not to. Lots of small elements and space overhead matters? Avoid
list and forward_list.
Need random access to elements: vector or deque. Insert or delete elements in the middle: list or forward_list. Insert or delete elements only at the front and back: deque. More complex scenarios:
Insert elements in the middle only while reading input, and subsequently needing random access.
Possibly just use vector anyway with elements to be added at the end, following by a call to sort it once the input is finished.
Or, consider using a list for the input phase and then copy the list into a vector.
#include <iostream>
#include <vector>
using namespace std;
intArray vector<int>
int main()
{
size_t size;
cout << "Enter the size of the container: ";
cin >> size;
// get space for size integers and initialize them to 0
vector<int> intArray( size );
for(int i=0; i<size; ++i)
intArray[i] = i;
}
The variable size is taken care of. No need to use dynamic memory allocation explicitly.
#include <array>
There are a few different ways to set up an array...
array<int, 3>
array<int, 3>
array<int, 3>
array<string,
a1{ {1, 2, 3} }; // Initial C++11
a2{1, 2, 3};
// Revised C++11
a3 = {1, 2, 3};
2> a4 = {string("a"), "b" };
If you are initially unsure, code for list and vector.
Only use functionality that works for both.
Use iterators, not subscripts, and avoid random access.
This will make it easier to go with either once you have enough information to decide.
Across the 8 types, the 3 different parameters are:
Set or map, in the former the key is the value while in the latter there are key-value pairs.
Unique keys or multiple keys.
Ordered or not.
set<string> exclude = {“the”, “but”, “and”, “or”, “an”, “a”};
map<string, string> authors = {
{“A-last”, “A-first”},
{“B-last”, “B-first”},
{“C-list”, “C-first”},
{key, value}
};
Elements within a map are pairs. This is defined in the header utility. A pair takes two type names, and the data elements of the pair have the corresponding types. For example,
pair<string, string> writers;
pair<string, string> musician{“Billy”, “Joel”};
Unusually, the data members of a pair are public, so you need to be careful where you use these. To access the data members: Use first, second.
musician.first;
musician.second;
It makes sense to use unordered associative containers if there isn’t an obvious ordering among elements, or it’s expensive to maintain whatever ordering could be applied. The hash function maps keys to buckets, which may contain a single value or multiple values.