How to Learn STL Containers
The container classes of STL make programming productive, safe and robust. They're smart arrays that perform memory allocation automatically, can resize, let you pass variables by reference, insert elements at any point, all with blazing efficiency. They provide a good reason to switch from C with its dumb arrays to C++. STL offers two types of container classes: simple and associative, where a key is associated with each stored object.
Things You'll Need
- Basic knowledge of C++
- A C++ compiler and preferably an integrated development environment
Instructions
-
-
1
Read and optionally memorize the two categories of container classes. Memorizing them never hurt anyone and will be of benefit to you in the long run.
Simple Containers: vector<>, lists<>, stack<>, queue<>, deque<>
Associative Containers: map<>, set<>, multimap<>, multiset<> -
2
Learn about all container classes by being shown how a Simple container can be used. This tutorial will illustrate the methods of vector<>.
-
-
3
Assign a size to the vector. You can assign vector size using any of the overloaded constructors. In addition, you can use the reserve() or assign() method after you've declared a vector. All the methods are shown below:
vector< int > arr(50); // specify the capacity
vector< int > arr(50, 17); // specify the capacity and give all elements a default value (17)
vector< int > arr2(arr); // initialize a vector from another vector
vector< int > vec;
vec.reserve(100); // or use reserve() to allocate memory -
4
Iterate across a vector. You can do this by using the standard index [] operator or through iterators, which are a special STL feature. Their capabilities are beyond the scope of this tutorial. The following code snippets demonstrate the size(), begin() and end() members:
// using standard indexing
for(int i = 0; i <= vec.size(); i++) {
cout<
}
// using iterators
for(vector<>::iterator iter = vec.begin(); iter != vec.end(); iter++) {
cout<<*iter<
} -
5
Add an element at the end, remove that element, insert an element in the middle and return the container size. You can perform all these functions by using the push_back(), pop_back(), insert() and size() methods, respectively, as shown below:
vec.push_back(35);
vec.pop_back();
vec.insert(pos, 19); // pos is the location
cout< -
6
Erase the elements within a range, erase the rest of the elements and make sure that the vector is empty. For these, you use the member functions erase(), clear() and empty().
vec.erase(vec.begin() + 5, vec.end() - 5); // erase all elements except the first and last 5
vec.clear(); // erase all elements
if (true == vec.empty()) {...} // check if vector is empty -
7
Check out all the member functions offered in vector<>. Many are common across the rest of the container classes:
_Destroy(), _Eq(), _Lt(), _Ucopy(), _Ufill(), assign(), at(), begin(), back(), capacity(), clear(), empty(), end(), erase(), front(), get_allocator(), max_size(), insert(), operator=, operator[], pop_back(), push_back(), rbegin(), rend(), reserve(), resize(), size(), swap(), ~vector(). -
8
Make sure you include the vector.h header file at the top of your source file before you test this code for yourself. Each container class has its own header file. A list<> will require list.h, a queue<> will require queue.h, and so on.
-
1
Tips & Warnings
The container classes go hand in hand with the algorithm functions, another powerful feature of the Standard Template Library.