[go: up one dir, main page]

0% found this document useful (0 votes)
6 views12 pages

STL Concept

The document provides a comprehensive guide on using vectors, sets, multisets, and maps in C++. It includes declarations, initializations, accessing and manipulating elements, and various functions and shortcuts for each data structure. Additionally, it covers unordered sets and maps, along with examples and notes on their properties and functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

STL Concept

The document provides a comprehensive guide on using vectors, sets, multisets, and maps in C++. It includes declarations, initializations, accessing and manipulating elements, and various functions and shortcuts for each data structure. Additionally, it covers unordered sets and maps, along with examples and notes on their properties and functionalities.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

VECTORS

DECLARATION:
vector<datatype>vector_name;
vector<datatype>vector_name(number of elements);
INITIALIZATION:
1.Direct :
vector<datatype>vector_name={elements}; // Valid
vector<datatype>vector_name(number of elements)={elements}; // Invalid
2. vector<datatype> a(10,7); // Initializes all elements with 7
3.Using push_back:
vector_name.push_back(element);
4.Using for loop:
for(int i=0;i<n;i++)
{
int a;
cin>>a;
vector_name.push_back(a);
}
5.Using range-based for loop:
// For using this,size of vector should be mentioned above
for(auto &ite:vector_name)
cin>>ite;
6.Initializing a vector with a copy of another vector
std::vector<int> vec2;
// Resize the destination vector to have the same size as the source vector
vec2.resize(vec1.size());
1
// Copy the elements from vec1 to vec2
copy(vec1.begin(), vec1.end(), vec2.begin());
7.Initializing a vector by using assign
vec2.assign(vec1.begin(),vec1.end());

ACCESSING/MANIPULATING ELEMENTS:
1.Same as array vector_name[];
2.Using iterator
// Declare iterator first: vector<datatype>::iterator iterator_name;
// *iterator_name denotes element at iterator_name position in the vector
3.Using range based for loop
/*
for(auto ite:vector_name)
cout<<ite;
*/
SHORTCUTS:
1.vector_name.begin()
2.vector_name.end()
3.vector_name.push_back(enter element to insert)
4.vector_name.pop_back() //removes last element in the vector
5.vector_name.empty() //returns boolean value by checking vector is empty or
not
6.vector_name.clear() //clears all elements in the vector
7.vector_name.erase(enter position /only iterator/);
/*
vector_name.erase(position-iterator);// for deletion at specific position

2
vector_name.erase(starting_position, ending_position);// for deletion in
range
*/
8.*min_element(lower iterator,upper iterator);
9.*max_element(lower iterator,upper iterator);
10.erase(unique(vec.begin(), vec.end()),vec.end());
11.sort(first_iterator,last_iterator) // -> It arranges the data in ascending order
or in increasing order.
12.reverse(first_iterator,last_iterator) // -> It reverses the arrangement of the
elements in the container.
13.binary_search(lower boundary,upper boundary,key) // return boolean value
14.count(lower iterator,upper iterator,x) // -> It returns the number of times ‘x’
has appeared in the vector.
15.find(lower iterator,upper iterator,x) // -> It returns the pointer to the end of
the vector if element ‘x’ is not present in the vector or returns the position of
the target element.
/*
vec<int>A={1,1,1,1,1,1,2,2,2,2,3,3,3,3,5,88,8,8,9,9};
vec<int>::it p;
A.erase(unique(A.begin(),A.end()),A.end()); // To remove duplicates
p=find(A.begin(),A.end(),9);
if(p==A.end())cout<<"NOT PRESENT";
else cout<<"PRESENT";
return 0;
*/
16.size(vector_name) or vector_name.size()
17.emplace(pos,value to insert)
18.emplace_back(value to insert)

3
19.vector_name.back()
20.vector_name.max_size()
21.vector_name.max_capacity()
22.vector_name.insert(position,val)
23.vector_name.insert(position,size,val)
24.vector_name.insert(position,iterator1 of another container,iterator2 of
another container)
25.vector_name.assign(iterator 1 of another vector,iterator 2 of another
vector);
TRANSFERING AN ARRAY ELEMENTS INTO VECTOR:
1.Using a range constructor
int n = sizeof(arr) / sizeof(arr[0]);
vector<int>vector_name(arr, arr + n);
2.Using the assign method
int n = sizeof(arr) / sizeof(arr[0]);
vector<int>vector_name;
vector_name.assign(arr, arr + n);
3.Using a loop
int n = sizeof(arr) / sizeof(arr[0]);
vector<int> vec;
for(int i=0;i<n;i++){vec.push_back(arr[i]);}
Note: Elements can be stored as pairs in vectors as shown:
#include <iostream>
#include <vector>
#include<string>
#include <utility> // for using pair,utility header file is to be used
using namespace std;

4
int main() {
// Declare a vector of pairs of string and int
vector<pair<string, int>> myVec;

// Add some pairs to the vector


myVec.push_back(make_pair("John", 25));
myVec.push_back(make_pair("Mary", 30));
myVec.push_back(make_pair("Tom", 20));

// Access and print the pairs using indexing


cout << "Pair at index 0: " << myVec[0].first << " is " << myVec[0].second << "
years old" << endl;
cout << "Pair at index 1: " << myVec[1].first << " is " << myVec[1].second << "
years old" << endl;
cout << "Pair at index 2: " << myVec[2].first << " is " << myVec[2].second << "
years old" << endl;

// Iterate over the vector and print all pairs


cout << "All pairs:" << endl;
for (auto const& pair : myVec) {
cout << pair.first << " is " << pair.second << " years old" << endl;
}
return 0;
}

5
SETS
DECLARATION:
1.set<data_type>set_name;//ascending order
2.set<data_type,greater<data_type>>set_name;//descending order
PROPERTIES:
1.Storing order – The set stores the elements in sorted order.
2.Values Characteristics – All the elements in a set have unique values.
3.Values Nature – The value of the element cannot be modified once it is added
to the set, though it is possible to remove and then add the modified value of
that element. Thus, the values are immutable.
4.Search Technique – Sets follow the Binary search tree implementation.
5.Arranging order – The values in a set are unindexed.
INITIALIZATION:
1.Direct:
set<data_type>set_name={elements};
2.Using insert
// If you use for(auto &p:set_name) , remove '&' and don't specify size earlier
set_name.insert(element);
3.Using a range constructor(at the time of declaration)
set<data_type>set_name(vector_name.begin(),vector_name.end());
4.Using assign//after declaration
set_name.assign(vector_name.begin(),vector_name.end());
5.Initializing a set with a copy of another set
set<int> s2(s1);//set s1 is copied to s2
6.Initializing a set with emplace() method:
/*set<int> s;

6
s.emplace(1);
s.emplace(2);
s.emplace(3);
s.emplace(4);
*/
7.Transfer an array into set
int arr[] = {1, 2, 3, 4, 5};
set<int> set;
// Transfer the elements from the array to the set
for(int i : arr){set.insert(i);}
SOME IMPORTANT TRANSFORMATIONS:
1.copying elements from set into a vector using copy
// Resize the destination vector to have the same size as the source set
vec.resize(set.size());
// Copy the elements from set to vec
copy(set.begin(), set.end(), vec.begin());
2.copying elements from set into a vector using assign
std::vector<int> vec;
// Assign the elements from set to vec
vec.assign(set.begin(), set.end());
ACCESSING ELEMENTS IN A SET:
1.Use only iterators
SHORTCUTS:
1.set_name.insert(element);
2.set_name.begin();
3.set_name.end();

7
4.set_name.empty();
5.set_name.size();
6.set_name.erase(iterator);
7.set_name.find(element);//returns set.end() if not found
/*
auto it = myset.find(10);
if (it != myset.end())
{
// element found
}
else
{
// element not found
}
*/
8.set_name.count(element);
9.set_name.lower_bound(value) //returns iterator
//if not there returns iterator to end
10.set_name.upper_bound(value) //returns iterator
//if not there returns iterator to end
UNORDERED SETS
It is a container of unique elements not in sorted order and not in given order
also(it chooses some random order)
DECLARATION:
unordered_set<data_type>set_name;
Remaining all functions are same like sets.

8
MULTI SETS
A multiset is a container that stores elements in a sorted order, allowing
duplicates. It is part of the C++ Standard Template Library (STL) and is
implemented in the "set" header file.
DECLARATION:
multiset<data_type>set_name
Multiset can be used to count the occurences of an element
set_name.count(key);
Remaining all functions are same like sets.
➔ For accessing minimum element in multiset *s.begin()
➔ For accesing maximum element in multiset *s..rbegin()
UNORDERED MULTISETS
Unordered multiset in C++ is a type of unordered associative container
that can have duplicate elements, and it is very similar to the unordered_set
data structure.
DECLARATION:
unordered_multiset<data_type>set_name;

Problems on sets:
https://codeforces.com/contest/1833/problem/A

9
MAPS
In maps, elements are stored in sorted(basis as key) order not allowing
duplicates;
DECLARATION:
map<data_type(of key),data_type(of value)>map_name;
INITIALIZATION:
1.Direct

map<data_type,data_type>map_name={{key,val},{key,val},{key,val},{key,val},{ke
y,val},{key,val}};
2.By using insert
map_name.insert(pair<data_type,data_type>(key,val));
map_name.insert(make_pair(key,val)); // make_pair represents
“pair<data_type,data_type>”
3.Copying from another map
map<data_type,data_type>map_name=map2; // map2 is copied into
map_name
4.Using iterators to initialize the map from a range of elements in another
container
vector<pair<string, int>> myVec = {{"John", 25}, {"Mary", 30}, {"Tom", 20}};
// Tuple is used when we need to club 3 or more elements
make_tuple is used
To access tuple elements we use get function i.e., get<0>(*it)
get<0>it

vector<tuple<string,int,int>>myVec={{“Shailesh”,1,2},{“Sreekar”,2,3},{“Shashan
k”,3,4}};
map<string, int> myMap5(myVec.begin(), myVec.end());

10
5.Direct by assigning
map_name[key]=value;
ACCESSING ELEMENTS IN MAPS:
1.Range based for loop
for (auto p : myMap)
{
cout << p.first << “ ”<< p.second << endl;
}
2.Using iterator
map<data_type,data_type>::iterator p; // Declaring iterator
for(p=map_name.begin();p!=map_name.end();p++)
{
cout<<p->first<<" "<<p->second<<endl;
}
3.Direct by key
cout<<map_name[key]; // prints value associated with key
4.Using find()
it=map_name.find(key); // returns iterator to end if not present else
points to the position

5. Using Map for Frequency


map_name[x]++;
NOTE: We can directly define an iterator using auto
/*
for (auto it = myMap.begin(); it != myMap.end(); ++it)

11
{
cout << "Key: " << it->first << ", Value: " << it->second << endl;
}
*/

Checkout: multimap & unordered_map & unordered_multimap


Same as maps but with special functionalities

NOTE:
If there is no required key element in map and you are trying to access it
. It’s value is defaulty set to zero;

ll i;
map<char,ll>a;
for(i=1;i<=10;i++)
{
cout<<a[i]<<" ";
}

12

You might also like