You are on page 1of 8

NON ELAB WEEK 11

RA2111003011353 F2

A SAI VIGNESH

1) Write a C++ Program to demonstrates Strings in STL.

CODE:

#include <iostream>

#include <string>

using namespace std;

int main()

string str;

getline(cin, str);

cout << "The initial string is : ";

cout << str << endl;

str.push_back('s');

cout << "The string after push_back operation is : ";

cout << str << endl;

str.pop_back();

cout << "The string after pop_back operation is : ";

cout << str << endl;

return 0;

OUTPUT:
2) Write a C++ Program to demonstrate Arrays in STL.

CODE:

#include <algorithm>

#include <array>

#include <iostream>

#include <iterator>

#include <string>

using namespace std;

int main() {

array<int, 5> ar1{{3, 4, 5, 1, 2}};

array<int, 5> ar2 = {1, 2, 3, 4, 5};

array<string, 2> ar3 = {{string("a"), "b"}};

cout << "Sizes of arrays are" << endl;

cout << ar1.size() << endl;

cout << ar2.size() << endl;

cout << ar3.size() << endl;

cout << "\nInitial ar1 : ";

for (auto i : ar1)

cout << i << ' ';

sort(ar1.begin(), ar1.end());
cout << "\nsorted ar1 : ";

for (auto i : ar1)

cout << i << ' ';

ar2.fill(10);

cout << "\nFilled ar2 : ";

for (auto i : ar2)

cout << i << ' ';

cout << "\nar3 : ";

for (auto &s : ar3)

cout << s << ' ';

return 0;

OUTPUT:

3) Write a C++ Program to demonstrate lists in STL.

CODE:

#include <iostream>

#include <iterator>

#include <list>

using namespace std;


// function for printing the elements in a list

void showlist(list<int> g)

list<int>::iterator it;

for (it = g.begin(); it != g.end(); ++it)

cout << '\t' << *it;

cout << '\n';

// Driver Code

int main()

list<int> gqlist1, gqlist2;

for (int i = 0; i < 10; ++i) {

gqlist1.push_back(i * 2);

gqlist2.push_front(i * 3);

cout << "\nList 1 (gqlist1) is : ";

showlist(gqlist1);

cout << "\nList 2 (gqlist2) is : ";

showlist(gqlist2);

cout << "\ngqlist1.front() : " << gqlist1.front();

cout << "\ngqlist1.back() : " << gqlist1.back();

cout << "\ngqlist1.pop_front() : ";

gqlist1.pop_front();
showlist(gqlist1);

cout << "\ngqlist2.pop_back() : ";

gqlist2.pop_back();

showlist(gqlist2);

cout << "\ngqlist1.reverse() : ";

gqlist1.reverse();

showlist(gqlist1);

cout << "\ngqlist2.sort(): ";

gqlist2.sort();

showlist(gqlist2);

return 0;

OUTPUT:

4) Write a C++ Program to demonstrate Unordered_set in STL.

CODE:

#include <bits/stdc++.h>
using namespace std;

int main()

unordered_set <string> stringSet ;

stringSet.insert("code") ;

stringSet.insert("in") ;

stringSet.insert("c++") ;

stringSet.insert("is") ;

stringSet.insert("fast") ;

string key = "slow" ;

if (stringSet.find(key) == stringSet.end())

cout << key << " not found" << endl << endl ;

else

cout << "Found " << key << endl << endl ;

key = "c++";

if (stringSet.find(key) == stringSet.end())

cout << key << " not found\n" ;

else

cout << "Found " << key << endl ;

cout << "\nAll elements : ";

unordered_set<string> :: iterator itr;

for (itr = stringSet.begin(); itr != stringSet.end(); itr++)

cout << (*itr) << endl;

OUTPUT:
5) Write a C++ Program to demonstrate pairs in STL.

CODE:

#include <iostream>

#include <utility>

using namespace std;

int main()

pair<int, char> PAIR1;

PAIR1.first = 100;

PAIR1.second = 'G';

cout << PAIR1.first << "";

cout << PAIR1.second << endl;

return 0;

OUTPUT:

You might also like