Technical Interview Questions and Answers :: C++
1 / 187
Sequence container stores elements in a linear sequence like a line .Each elements are related to other by its position along the line. They all expand themselves to allow insertion to the elements and all of them support a number of operations on them.
Element0 -> Element1 -> Element2 -> …….. -> Last element…
/\
|
Begin() end()
2 / 187
Associative containers are designed to support direct access to elements using keys. They are not sequential. There are 4 types of container:
• set
• multiset
• multimap
• map
All these containers stores data in a structure called tree which facilities fast searching, deletion and insertion.
3 / 187
The STL provides three derived containers namely:
Stack, queue and priority queue. These are known as Container adaptors.
These can be created for different sequence container and it does not support the Iterators and therefore we cannot use them for data manipulation.
However, they support two member functions pop() and push() fro implementing deleting and inserting operation.,
4 / 187
Algorithms are a function that can be used generally across a variety of containers for processing their contents.
5 / 187
STL algorithm reinforce the philosophy of reusability. By using these algorithms, programmers can save a lot of time and effort.
To access STL Algorithm we must be included
6 / 187
STL algorithm based on the nature of operations it may be categorized s follows:
• retrieve or non-mutating algorithm
• Mutating algorithm
• Sorting algorithm
• Set algorithm
• Relational algorithm
7 / 187
The list is another container that is popularly used. It supports bidirectional, linear list and provides an efficient implementation for deletion and insertion operation. Unlike a vector, which supports random access, a list can be accessed sequentially only.
Write some important member functions of the list class?
Function Task
Back() Gives a reference to the last element
Begin() Gives a reference to the first element
Clear() Deletes all the elements
Empty() Decides if the list is empty or not
End() Gives refernce to the end of the list
Erase() Deletes elements as specified
merge() Merge two ordered list
Insert() Insert elemements as specified
Pop_back() Deletes the last element
Pop_front() Deletes the first element
Push_back() Adds an element at the end
Push_front() Adds an element at the front
8 / 187
A map is a sequence of key value pairs that provides for fast retrieval based on the flag. Almost one value is held for each key; in other words, each key in a map is unique.
9 / 187
A multimap is a map, except that it allows duplicate keys. Naturally several values can exist for a single key. A multimap is preferred over a map. In some ways, a multimap is even cleaner and more elegant than a map.
10 / 187
Iteration over a map is simply iteration over a sequence of pair < const key, mapped_type >. For e.g. We might print out the entries of a phone book like this
Void f(map
Typedef map(string, number)::const_iteration CI;
For(CI =phone_book.begin( ); p!=phone_book.end( );++p)
cout<
}
A map iterator presents the elements in ascending order of its keys.