L8a STL Intro
L8a STL Intro
Programming*
2
Ultimate goal
3
Competitive Programming
4
How?
5
Some Good Books
6
The problems
7
Example problem*
Problem description
Write a program that multiplies pairs of integers.
Input description
Input starts with one line containing an integer , where ,
denoting the number of test cases. Then lines follow, each
containing a test case. Each test case consists of two integers ,
where , separated by a single space.
Output description
For each test case, output one line containing the value of
11
Tips
• There are a couple of tips and guidelines you can keep in mind
towards becoming a more effective programmer and better
problem solver.
12
Tip 0: Faster typing*
1/27/2025 14
Tips: Master programming languages
1/27/2025 15
Tips: Master programming languages
• Java:
– Scanner, BigInteger, String static functions,
– Collections, different data types
– Integer.parseInt()
– String.substring()
– etc
• C++
– sort(), vector<T>, STL
• C
– qsort(), malloc(), …
17
Most Important Tip
1/27/2025 18
C++: STL
• Standard Template Library
• You have already studied templates., STL is extension to
that
• Collections of useful classes for common data structures
• Ability to store objects of any type (template)
• Containers form the basis for treatment of data
structures
• Container –class that stores a collection of data
• STL consists of 10 container classes:
– Sequence containers
– Adapter containers
– Associative containers
1/27/2025 19
C++: STL
• The standard template library (STL) contains
– Containers
– Algorithms
– Iterators
• A container is a way that stored data is organized in
memory, for example an array of elements.
• Algorithms in the STL are procedures that are applied to
containers to process their data, for example search for
an element in an array, or sort an array.
• Iterators are a generalization of the concept of pointers,
they point to elements in a container, for example you
can increment an iterator to point to the next element in
an array 20
C++: STL
• https://www.topcoder.com/community/data-
science/data-science-tutorials/power-up-c-with-the-
standard-template-library-part-1/
• https://www.topcoder.com/community/data-
science/data-science-tutorials/power-up-c-with-the-
standard-template-library-part-2/
• Good details are given in IIITD’s ppt on STL
• Let us have a look
• I started using STL by copy-paste of simple codes like
vectors, map/hash (let us have a look in DevCpp, code-
generation settings changed to have language standard:
ISO C++ 11)
21
#include <bits/stdc++.h>
/*either include bits/stdc++.h or include indiv header files like
#include<iostream>
#include<vector> */
using namespace std;
int main()
{ // Initializing vector with values
vector<int> vect1{1, 2, 3, 4};
vector<int> vect2; // Declaring new vector
// copy elements of old vector into new vector by Iterative method
for (int i=0; i<vect1.size(); i++)
vect2.push_back(vect1[i]);
cout << "Old vector elements are : ";
for (int i=0; i<vect1.size(); i++)
cout << vect1[i] << " ";
}
Practice Using STL
• Using stack STL, Find whether a given string is palindrome
or not
• Using vector, find average of all numbers, T times, where
each time user tells some new elements or deletes some
old elements. Use resize, shrink_to_fit , reserve aetc.
• Use hash_table and map of STL and solve any simple
problem
24
int T,n,val1; vector<int> vect1;
ifstream infile ("in1.txt"); ofstream outfile("out1.txt");
if(!infile){cout<< "input file missing\n"; return -1;}
infile >> T;
while (T--)
{ infile >>n;
for(int i = 0; i < n; i++)
{ infile >> val1;
vect1.push_back(val1); }
vector<int> vect2;
for (int i=vect1.size() -1; i>=0; i--)
vect2.push_back(vect1[i]);
outfile << "Vector 2: Old vector elements in reverse order are ";
for (int i=0; i<vect1.size(); i++)
outfile << vect2[i] << " ";
outfile << endl;
vect2.clear();//is of no use as recreated everytime, but vect1 gets appended
(not over-written with new inputs in each iterations
//vect1.clear(); //clear vect1 for no append to previous inputs