How To Troubleshoot Collaboration Apps for the Modern Connected Worker
List
1. Level up your coding skills with
the C++ Standard Template
Library (STL):
List
BY JOYJIT CHOUDHURY
2. List
List is a sequence container that allows constant time
insertion and deletions anywhere in the container
It allows iterations in both directions
Fast random access is not supported
List containers are implemented as Doubly Linked List
Defined in the header <list>
Belongs to the namespace std
4. Accessing the elements
Member functions front() and back(), returns a
reference to the first and last element of the list,
respectively
front() back()
5. Accessing the elements
front() and back() return a reference, so not only can
you view the first and last element, you can also modify
them
list<int> mylist
6. Insert elements
Member functions push_front() and push_back(), inserts an element
to the beginning and the end of the list, respectively
7. Delete elements
Member functions pop_front() and pop_back(), removes the first
and the last element of the list, respectively
8. Size of the list
Member function size() returns the number of elements in the list
Time Complexity : O(n)
9. Is the list empty?
Member function empty() returns true if the list is empty (i.e. list size is 0),
else returns false
Time Complexity : O(1)
It’s a preferred way to check
whether a list is empty,
instead of checking whether
the size of the list is 0, since
this takes constant time
10. So, how do you Traverse a list?
The thing about containers is, you hardly know anything about it’s internal
mechanisms
All the internal details regarding :
How the memory is allocated
Where and how the data elements are stored
How the insertion, access and removal of individual elements take place
It’s all kept hidden from the user.
So, what if you had to access the individual elements, maybe even modify
them?
For that purpose, containers have what is known as Iterators
11. What are Iterators?
An iterator is an object, pointing to some element in a range of
elements (usually, a container), which has the ability to iterate through
the elements in that range through a set of operators
(increment (++) operator, dereference (*) operator, etc)
You can think of them as an abstraction of pointers
What that means is, they are like pointers in certain ways, but not
exactly
12. Iterators
The best way to understand iterators, would be through an example
list<int> myList
myList.begin()
14. Insert elements anywhere in the list
Member function insert(), inserts new elements before the element at
the specified position. The position is specified by passing an iterator as a
parameter.
15. Delete elements from anywhere
Member function erase(), removes from the list, either a single element from
the specified position, or all the elements in the specified range [first, last).
16. That’s not all. There are many other functions
and techniques that could come in handy.
Read about them on cplusplus.com or
cppreference.com or somewhere else. Just
google it!