CONST Pointer vs Pointer to CONST in c.

Type * var;
_ Type _ * _ var;
Any _ underscore can be replaced with const in above statement.
below are three valid statements.

const int *ptr; // ptr is a pointer to constant int
int const *ptr; // ptr is a pointer to constant int (misleading but valid statement)

The Clockwise/Spiral Rule

Example #1: Simple declaration

                                  +-----------+
                                  |   +-----+ |
                                  |   | +-+ | |
                                  |   | ^ | | |
                            int const *ptr; | |
                             ^    ^   ^   | | |
                             |    |   +---+ | |
                             |    +---------+ |
                             +----------------+

  • Question we ask ourselves: What is ptr?
  • ptr is an…

  • We move in a spiral clockwise direction starting with `ptr’ and we see the end of the line (the `;’), so keep going  ptr is an …
  • Continue in a spiral clockwise direction, and the next thing we encounter is the `*’ so, that means we have pointers, so… ptr is an pointer to…
  • Continue in a spiral direction and we see the const so ptr is an pointer to constant …
  • Continue in a spiral direction and we see the int

ptr is an pointer to constant int.

Example #2: Simple declaration


                                  +--------------+                       
                                  |   +--------+ |
                                  |   |    +-+ | |
                                  |   |    ^ | | |
                            int   * const ptr; | |
                             ^    ^   ^      | | |
                             |    |   +------+ | |
                             |    +------------+ |
                             +-------------------+

Question we ask ourselves: What is ptr? ptr is an...
  • We move in a spiral clockwise direction starting with `ptr’ and we see the end of the line (the `;’), so keep going

    ptr is an …

  • Continue in a spiral clockwise direction, and the next thing we encounter is the const so, that means we have constant, so…

    ptr is an constant …

  • Continue in a spiral direction and we see the * so

    ptr is an constant pointer to …

  • Continue in a spiral direction and we see the int

ptr is an constant pointer to int

Example #3: Simple declaration

                             +-----------+
                                  |   +-----+ |
                                  |   | +-+ | |
                                  |   | ^ | | |
                           const int  *ptr; | |
                             ^    ^   ^   | | |
                             |    |   +---+ | |
                             |    +---------+ |
                             +----------------+

Question we ask ourselves: What is ptr?

ptr is an…

  • We move in a spiral clockwise direction starting with `ptr’ and we see the end of the line (the `;’), so keep going

    ptr is an …

  • Continue in a spiral clockwise direction, and the next thing we encounter is the `*’ so, that means we have pointers, so…

    ptr is an pointer to…

  • Continue in a spiral direction and we see the int so

    ptr is an pointer to int …

  • Continue in a spiral direction and we see the const
        ptr is an pointer to int constant.
     So   ptr is an pointer to constant int also means same thing in English as well as in c.

Note: Spiral rule can be applied on any c declaration.

C++ STL Vector

Declaration Syntax:

int n = 10, m = 20;    
vector<int> v = {7, 5, 16, 8};
vector<int> vec(10); //with initial size 10, vec[i] can be used to insert elements.
vector<int> vec2(10, 0); //intialized with zero.
vector<int> vec3(n);
vector<int> vec4(n, 0);
vector<vector<int>> grid(n, vector<int>(m)); // 2D vector of nxm
vector<vector<int>> grid2(n, vector<int>(m, 0)); //initialized with 0
vector<vector<int>> twoDVector;

Iterators

  1. begin() : returns an iterator pointing to the first element of the vector.
  2. end()  : returns an iterator pointing to the theoretical element that follows the last element of vector.
  3. rbegin() : returns a reverse iterator pointing to the last element of vector (reverse beginning). It moves from last to first element.
  4. rend() : returns a reverse iterator pointing to the the pseudo(fake) element that precedes first element of vector (considered as reverse end).
  5. cbegin() : returns a constant iterator pointing to the first element of vector.
  6. cend() : returns a constant iterator pointing to the pseudo(fake) element that follows the last element of vector.
  7. crbegin() : returns a constant reverse iterator pointing to the last element of vector (reverse beginning). It moves from last to first element
  8. crend() : returns a constant reverse iterator pointing to the pseudo(fake) element  that precedes the first element of vector (considered as reverse end)

Member Functions:

  1. size() : returns the number of elements of vector.
  2. max_size() : returns the maximum possible size the object of vector class can have. 
  3. capacity() : returns the size of the storage space currently allocated to the vector expressed as number of elements.
  4. resize() : resizes the container so that it contains n elements.
  5. empty() : returns whether the container is empty.
  6. shrink_to_fit() : reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
  7. reserve() : requests that the vector capacity be at least enough to contain n elements.

size() vs max_size() vs capacity():

 vector<int> vec;
vec.push_back(1);
cout <<"size: " << vec.size() << " max_size: " << vec.max_size() << " capacity: " << vec.capacity()<<endl;
vec.push_back(2);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;
vec.push_back(3);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;
vec.push_back(4);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;
vec.push_back(5);
cout << "size: " << vec.size() << " max_size: " << vec.max_size()<< " capacity: " << vec.capacity() << endl;

 

Output:

size: 1 max_size: 4611686018427387903 capacity: 1

size: 2 max_size: 4611686018427387903 capacity: 2

size: 4 max_size: 4611686018427387903 capacity: 4

size: 3 max_size: 4611686018427387903 capacity: 4

size: 5 max_size: 4611686018427387903 capacity: 8


Element access:


  1. reference operator [index] : returns a reference to the element at position ‘index’ in the vector

  2. at(index) : returns a reference to the element at position ‘index’ in the vector

  3. front() : returns a reference to the first element in the vector.

  4. back() : returns a reference to the last element in the vector.

  5. data() : returns a direct pointer to the memory array used internally by the vector to store its owned elements.




Modifiers:


  1. assign() – It assigns new value to the vector elements by replacing old ones

  2. push_back() – It push the elements into a vector from the back

  3. pop_back() – It is used to pop or remove elements from a vector from the back.

  4. insert() – It inserts new elements before the element at the specified position

  5. erase() – It is used to remove elements from a container from the specified position or range.

  6. swap() – It is used to swap the contents of one vector with another vector of same type. Sizes may differ.

  7. clear() – It is used to remove all the elements of the vector container

  8. emplace() – It extends the container by inserting new element at position

  9. emplace_back() – It is used to insert a new element into the vector container, the new element is added to the end of the vector


C++ STL Pair

 pair  Pair_name;
pair <int, string> pair_var;
pair_var.first = 1;
pair_var.second = "January";

pair <string, double> pair_var2;
pair_var2.first = "pi";
pair_var2.second = 3.14;

pair <string, float> golden_ratio_pair("goldenRatio", 1.6180);
pair <string, float> copy_pair(golden_ratio_pair);

cout<< golden_ratio_pair.first <<" : "<<golden_ratio_pair.second;
cout<< copy_pair.first <<" : "<<copy_pair.second;

pair <int, string> pair_var;

Output:
goldenRatio : 1.6180
goldenRatio : 1.6180

Member Functions

Pair_name = make_pair (value1,value2);
pair <string, int> pair_var3;
pair_var3 = make_pair("techBudhdha", 1);

pair <string, int> pair_var4("krishna", 2);
pair <string, int> pair_var5("techBudhdha", 1);
swap()

pair_var4.swap(pair_var5);orswap(pair_var4, pair_var5); Operators:operators(=, ==, !=, >=, <=, ) pair <int, int> pair_var1(5, 10); pair <int, int> pair_var2(7, 14); pair <int, int> pair_var3(5, 10); if (pair_var1 == pair_var3) // Trueif (pair_var1 == pair_var2) // Falseif (pair_var1 != pair_var2) // True <= and >= operator compare only first element of pair. if (pair_var1 <= pair_var2) //Actual thing in backGround: pair_var1.first <= pair_var2.first : Trueif (pair_var1 >= pair_var2) //Actual thing in backGround: pair_var1.first >= pair_var2.first : Falsesame for operators.

ptr