Vector Cotnainer Operations
Vector Implementation Example:
#include<bits/stdc++.h>
using namespace std;
vector<string> locations;
vector<string> identifiers[1000];
int searchLocation(string target){
for(int idx=0; idx<locations.size(); idx++){
if(locations[idx] == target)
return idx;
}
return -1;
}
int main(){
int count;
cin >> count;
for(int i=0; i<count; i++){
string id, location;
cin >> id >> location;
int position = searchLocation(location);
if(position == -1){
locations.push_back(location);
identifiers[locations.size()-1].push_back(id);
}
else {
identifiers[position].push_back(id);
}
}
for(int i=0; i<locations.size(); i++){
cout << locations[i] << " " << identifiers[i].size() << endl;
for(int j=0; j<identifiers[i].size(); j++)
cout << identifiers[i][j] << endl;
}
}
Queue Data Structure
Banking Queue Management Example:
#include<bits/stdc++.h>
using namespace std;
queue<string> priorityQueue;
queue<string> normalQueue;
int main(){
int operations;
cin >> operations;
while(operations--){
string action, customer, queueType;
cin >> action;
if(action == "IN"){
cin >> customer >> queueType;
if(queueType == "V")
priorityQueue.push(customer);
else
normalQueue.push(customer);
}
else{
cin >> queueType;
if(queueType == "V")
priorityQueue.pop();
else
normalQueue.pop();
}
}
while(!priorityQueue.empty()){
cout << priorityQueue.front() << endl;
priorityQueue.pop();
}
while(!normalQueue.empty()){
cout << normalQueue.front() << endl;
normalQueue.pop();
}
return 0;
}
Map Container
Language Processing Example:
#include<bits/stdc++.h>
using namespace std;
map<string, bool> wordMap;
int main(){
int wordCount;
string result = "No";
cin >> wordCount;
for(int i=0; i<wordCount; i++){
string term;
cin >> term;
if(wordMap.count(term)){
result = term;
break;
}
else {
wordMap[term] = true;
}
}
cout << result << endl;
return 0;
}
Set Container
Set Operatiosn Example:
#include<bits/stdc++.h>
using namespace std;
int totalElements;
int selectionCount;
set<int> integerSet;
set<string> stringSet;
int main(){
string first = "Example1";
string second = "Example2";
string third = "Example3";
stringSet.insert(third);
stringSet.insert(first);
printf("Elements after 2 insertions: %d\n", stringSet.size());
stringSet.insert(second);
printf("Elements after 3 insertions: %d\n", stringSet.size());
set<string>::iterator stringIterator;
for(stringIterator = stringSet.begin(); stringIterator != stringSet.end(); stringIterator++)
cout << *stringIterator << " ";
cout << endl;
if(stringSet.count(second) != 0)
cout << "Element " << second << " exists" << endl;
return 0;
}