CSE 2208 Lab Manual STL
User Manual:
Open the PDF directly: View PDF .
Page Count: 5
Download | ![]() |
Open PDF In Browser | View PDF |
Lab 1 – STL Objective: Objective of this topic is to learn about Standard Template Library of C++. This knowledge will be required throughout the course to code easily. Standard Template Library is a C++ library which contains some useful data structures like Stack, Queue, Vector, Priority Queue, Map etc. They come very handy while coding graph theory related codes. Vector Vectors are sequence container that can change size. Container is an object that holds data of same type. Sequence containers store elements strictly in linear sequence. Vector stores elements in contiguous memory locations and enables direct access to any element using operator []. Unlike array, vector can shrink or expand as needed at run time. The storage of the vector is handled automatically. Following is the way to declare vector type variable: vectorVARIABLE_NAME; The vector operations that will be used frequently in this course are: .push() - It is used to insert an element at the back of a vector. .size() - It is used to find out the number of elements kept inside the vector. To use vector we need to add #include . Example: You are given a list of N numbers in a vector. Write a program to find out if summation of all odd numbers in the vector is greater than summation of all even numbers. Sample Input Sample Output 5 12345 4 1234 YES NO Sample Code for Vector #include #include using namespace std; int main() { int n; scanf(“%d”, &n); vector v; for(int i = 0; i < n; ++i) { int a; scanf(“%d”, &a); v.push_back(a); } int odd = 0, even = 0; for(int i = 0; i < n; ++i) { odd += (v[i] % 2 == 0) ? 0 : v[i]; even += (v[i] % 2 == 0) ? v[i] : 0; } } if(odd > even) printf(“YES\n”); else printf(“NO\n”); Map Map is dictionary like data structure. It is a sequence of (key, value) pair, where only single value is associated with each unique key. It is often referred as associative array. Following is the way to declare map type variable: map VARIABLE_NAME; An example of storing data in a string, double pair map is given below: map mp; mp[“ABC”] = 10.203; mp[“EFG”] = -5.360; Now if we print mp[“ABC”], then 10.203 will be printed. And for mp[“EFG”] it will be -5.360. To use map we need to add #include
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : No Page Count : 5 Language : en-US Tagged PDF : Yes Author : Shibli Creator : Microsoft® Word 2013 Create Date : 2019:01:05 18:14:37+06:00 Modify Date : 2019:01:05 18:14:37+06:00 Producer : Microsoft® Word 2013EXIF Metadata provided by EXIF.tools