0% found this document useful (0 votes)
6 views

STL in CPP

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

STL in CPP

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Standard Template Library (STL)

Introduction to STL, STL Components, Containers- Sequence container and associative


containers, container adapters, Application of Container classes: vector, list,
Algorithms- basic searching and sorting algorithms, min-max algorithm, set operations, heap
sort, Iterators- input, output, forward, bidirectional and random access. Object Oriented
Programming – a road map to future

Algorithms- basic searching and sorting algorithms, min-max algorithm, set operations, heap
sort,

basic searching and sorting algorithms

binary_search
// binary_search example
#include <iostream> // std::cout
#include <algorithm> // std::binary_search, std::sort
#include <vector> // std::vector

bool myfunction (int i,int j) { return (i<j); }

int main () {
int myints[] = {1,2,3,4,5,4,3,2,1};
std::vector<int> v(myints,myints+9); // 1 2 3 4 5 4
3 2 1

// using default comparison:


std::sort (v.begin(), v.end());

std::cout << "looking for a 3... ";


if (std::binary_search (v.begin(), v.end(), 3))
std::cout << "found!\n"; else std::cout << "not found.\n";

// using myfunction as comp:


std::sort (v.begin(), v.end(), myfunction);

std::cout << "looking for a 6... ";


if (std::binary_search (v.begin(), v.end(), 6, myfunction))
std::cout << "found!\n"; else std::cout << "not found.\n";

return 0;
}
C++ STL std::minmax() function
minmax() function is a library function of algorithm header, it is used to find the smallest and largest values, it
accepts two values and returns a pair of the smallest and largest values, the first element of the pair contains
the smallest value and the second element of the pair contains the largest value.

Note:To use minmax() function – include <algorithm> header or you can simple
use <bits/stdc++.h> header file.

Syntax of std::minmax() function

std::minmax(const T& a, const T& b);

//C++ STL program to demonstrate use of


//std::minmax() function
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
int a = -10;
int b = -20;

//finding pair of smallest and largest numbet


auto result = minmax(a, b);

//printing the smallest and largest values


cout << "smallest number is: " << result.first << endl;
cout << "largest number is: " << result.second << endl;

return 0;
}

You might also like