STL Tutorial

https://www.cs.helsinki.fi/u/tpkarkka/alglib/k06/lectures/containers.html


Vector: 
  1. Vectors are sequence containers that have dynamic size. In other words, vectors are dynamic arrays. Just like arrays, vector elements are placed in contiguous storage location so they can be accessed and traversed using iterators.
  2. Vector provide random access.
  3. Insertions and deletions in the beginning or in the middle are slow.
#include <iostream>
#include <vector>
using namespace std;

int main() 

{
   vector<int> vec;
   int i;

   cout << "vector size = " << vec.size() << endl;

   for(i = 0; i < 5; i++) {
      vec.push_back(i);
   }

   cout << "extended vector size = " << vec.size() << endl;

   for(i = 0; i < 5; i++) {
      cout << "value of vec [" << i << "] = " << vec[i] << endl;
   }

   vector<int>::iterator v = vec.begin();
   while( v != vec.end()) {
      cout << "value of v = " << *v << endl;
      v++;
   }

   return 0;
}

List:
  1. A list is implemented as a doubly linked list.
  2. Lists do not provide random access.
  3. Fast insertion and deletion anywhere.
#include <iostream>
#include <list>

int main ()
{
  // constructors used in the same order as described above:
  std::list<int> first;                               
  std::list<int> second (4,100);                       // four ints with value 100
  std::list<int> third (second.begin(),second.end());  // iterating through second
  std::list<int> fourth (third);                     

  int myints[] = {16,2,77,29};
  std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are: ";
  for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
    std::cout << *it << ' ';

  std::cout << '\n';

  return 0;
}
 

Deque:
  1. deque is a dynamic array that is implemented so that it can grow in both directions.
  2. Inserting element at the end and at the beginning is fast. Inserting and elements in the middle, however, takes time because element must be moved.
  3. Insertion and deletion in the beginning in (amortized) constant time.
    Slower element access and iterators.
#include <iostream>
#include <deque>

int main ()
{
  unsigned int i;

  // constructors used in the same order as described above:
  std::deque<int> first;                                // empty deque of ints
  std::deque<int> second (4,100);                       // four ints with value 100
  std::deque<int> third (second.begin(),second.end());  // iterating through second
  std::deque<int> fourth (third);                       

  // the iterator constructor can be used to copy arrays:
  int myints[] = {16,2,77,29};
  std::deque<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are:";
  for (std::deque<int>::iterator it = fifth.begin(); it!=fifth.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}
 
Set:
  1. Sets are containers that store unique elements following a specific order.
  2. In a set, the value of an element also identifies it (the value is itself the key, of type T), and each value must be unique. The value of the elements in a set cannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.
  3. Internally, the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).
  4. set containers are generally slower than unordered_set containers to access individual elements by their key, but they allow the direct iteration on subsets based on their order.
  5. Sets are typically implemented as binary search trees.
#include<iostream>
#include<set>

int main()
{
    std::set<int> setOfNumbers;

    setOfNumbers.insert(1);
    setOfNumbers.insert(2);
    setOfNumbers.insert(3);
    setOfNumbers.insert(4);


    std::cout<<"Set Size = "<<setOfNumbers.size()<<std::endl;

    std::set<int>::iterator it;
    for (it=setOfNumbers.begin(); it!=setOfNumbers.end(); ++it)
        std::cout << ' ' << *it;
    std::cout<<"\n";
    return 0;
}

Comments

Popular posts from this blog

STL Questions

Producer Consumer problem using mutex

Interview questions