All the below STL's studied from STL
Containers and iterators:
Containers: are of four standard types
1. Sequence Container :
- Arrays: Contains static contiguous array
- Vector: Contains Dynamic contiguous array
- forward_list : Single linked list
- list: Double linked list
- deque: double ended queue, Where element can be added at the end or back of queue.
2. Container adaptor:
- stack : LIFO
- queue : FIFO
- priority queue : provide a priority queue, which allows for constant time look up of largest element
3. Associative containers : provides a sorted data structures that provides the faster O(log(n)) look up by using keys.
Key are unique:
- set: collection of unique keys, sorted by keys
- map : collection of key-value pairs , sorted by keys
Same key for multiple entries
- multiset: collection of keys sorted by keys
- multimap: collection of key value pairs, sorted by keys
4. Unordered associative containers provides a in sorted data structure that can be access using hash.
Key are unique:
- unordered_set: collection of keys, sorted by keys
- unordered_map : collection of key-value pairs , sorted by keys
Same key for multiple entries
- unordered_multiset: collection of keys sorted by keys
- unordered_multimap: collection of key value pairs, sorted by keys
Vector:
-Vector is a container template which is same as a dynamic array- values in vector will store in continues memory.
- Double the size when user wants to insert new element after exceeding its size
Current capacity of the vector can be read using the vectorobj.capacity()
example: create a vector of size 5 and intilize it with 1 std::vector<int> vobj(5, 1);
address vobj = [1,1,1,1,1], starting address can be vobj[0] -> 100, vobj[1] ->104, vobj[2] ->108, vobj[3] ->112, vobj[4] ->116 etc. here vobj.capacity() returns 5.
Insert 6th element into vobj i.e vobj.puch_back(2); then
new memory with 10 int size created with starting address i.e suppose 200.
then it copy all elements from address 100 to 200(i.e vobj[0] -> 200, vobj[1] ->204, vobj[2] ->208, vobj[3] ->212, vobj[4] ->216) and insert value 2 into vobj[5]->220.
and delete the old address memory
here vobj.capacity() returns 10.
- Advantage: fast in accessing the elements like array but its no need to be fixed size limitation like arrays. it automatically expand the size.
Vector Initialization:
1. Vector constructor accept the size of vector.
std::vector<int> vobj(5);
2. Vector constructor also accepts size of vector and particular value into vector
std::vector<int> vobj(5, 1); i.e vector contains all 1's as a default values stored to it [1,1,1,1,1]
3. initialize vector with an array vector obj(first addrss, last address)
int arr[] = {1,2,3,4,5};
std::vector<int> vobj(arr, arr+(sizeof(arr)/sizeof(arr[0]));
now vector contains [1,2,3,4,5]
std::list<int> listobj;
listobj.puch_back(1);
listobj.puch_back(2);
listobj.puch_back(3);
listobj.puch_back(4);
listobj.puch_back(4);vector<int> vobj(listobj.begin(), listobj.end());
5. initialize vector with other vector
vector<int> newvobj(vobj);
- Filling the vector with the random number using std::generate(v.begin(), v.end(), []{ return rand()/100});
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
struct RandomGenerator {
int maxValue;
RandomGenerator(int max) :
maxValue(max) {
}
int operator()() {
return rand() % maxValue;
}
};
int main() {
// Initialize a vector with 10 ints of value 0
std::vector<int> vecOfRandomNums(10);
// Generate 10 random numbers by lambda func and fill it in vector
std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(), []() {
return rand() % 100;
});
std::cout << "Random Number Generated by Lambda Function" << std::endl;
for (int val : vecOfRandomNums)
std::cout << val << std::endl;
// Generate 10 random numbers by a Functor and fill it in vector
std::generate(vecOfRandomNums.begin(), vecOfRandomNums.end(),
RandomGenerator(500));
std::cout << "Random Number Generated by Functor" << std::endl;
for (int val : vecOfRandomNums)
std::cout << val << std::endl;
return 0;
|
- For user defined classed if copy constructor and assignment operator are public then only one can insert its object to vector (its kind of why deap copy is required problem as vector will copy the content not as tease. ) refer user defined objects insertion into vector
- vectors are more efficient if removal and inserting happen in back end only. more info how to use vector efficiently
- vector iteration invalidation : once iterator used then insert new element again wants to use previously used iterator which may not point in same location as previous as if vector doubled happen and new location created. refer vector iterator invalidation
-
- vectors are more efficient if removal and inserting happen in back end only. more info how to use vector efficiently
- vector iteration invalidation : once iterator used then insert new element again wants to use previously used iterator which may not point in same location as previous as if vector doubled happen and new location created. refer vector iterator invalidation
-
No comments:
Post a Comment