C++ Std vs Stl
The full form of std is standard and it is a namespace. All the identifiers are declared inside the std namespace, in other words, a namespace provides scope to identifiers such as function names, variable names, etc. defined inside it. It is a feature especially available in C++ and is not present in C. The std keyword is used along with the space resolution operator (::) in every printing line and variable declaration.
Example:
std::cout<<"GeeksForGeeks"<<std::endl;
Basic Input Output Operations such as cin, cout are under <iostream> library of the std namespace. Apart from these, there are other headers such as <vector>, <pair>,<map>,<iostream>…etc. The Space resolution operator is used with:
- String
- Cout
- Cin
- endl
Example:
std::string s= "geeksforgeeks"; std :: cout<<s; std:: cin>>s; std::endl; Note: - If std:: is not given with the above four then an error will pop up.
Example:
C++
// C++ Program to run a program without using namespace std #include <iostream> // if not using the above we have to use std:: with print // statement and variable declaration int main() { std::string s = "GeeksForGeeks" ; std::cout << s; return 0; } |
GeeksForGeeks
Example:
C++
// C++ Program to run a program using namespace std #include <iostream> using namespace std; int main() { string s = "GeeksForGeeks" ; cout << s << endl; return 0; } |
GeeksForGeeks
STL in C++
STL is a collection of C++ template classes that provide common programming data structures, such as lists, stacks, arrays, etc. It includes classes for containers, algorithms, and iterators.
STL has 4 components:
- Algorithms
- Containers
- Functions
- Iterators
1. Algorithms
The header algorithm defines a collection of functions specially designed to be used on a range of elements. They act on containers and provide means for various operations for the contents of the containers. Algorithms Can be divided into mainly two categories:
- Non-Mutating Algorithm: Some algorithms listed inside this category are Find (), lower_bound(), upper_bound(),min_element(),max_element(), etc.
- Mutating Algorithm: Some algorithms listed inside this category are sort(), make_heap(), merge(), reverse(), next_permutation(), etc.
Example:
C++
// C++ program to depict the use of max_element in stl #include <bits/stdc++.h> using namespace std; int main() { int arr[]={1,3,4,6,7,8,9}; int * i1; i1 = std::max_element(arr , arr+ 7); cout<<*i1<<endl; return 0; } |
9
Example:
C++
// C++ program to depict the use of sort in stl #include <bits/stdc++.h> using namespace std; int main() { int arr[] = { 1, 3, 4, 6, 7, 8, 9 }; sort(arr, arr + 7); for ( int i = 0; i < 7; i++) cout << arr[i] << " " ; return 0; } |
1 3 4 6 7 8 9
2. Containers
Containers are used for storing data. Containers can be classified into mainly two major categories namely,
- Sequence Containers: This container consists of a dequeue, list, and vector.
- Associative Containers: This container consists of set, multiset, map, multimap, hash_set, hash_map, hash_multiset, and hash_multimap.
- Container Adaptors: This container consists of a queue, priority queue, and stack.
- Unordered Associative containers: – This container consists of unordered_set ,unordered_multiset,unordered_map ,unordered multimap
3. Functions
The STL includes classes that overload the function call operator. Instances of such classes are called function objects or functions. Functions allow the working of the associated function to be customized with the help of parameters to be passed.
Example:
C++
// C++ Program for function to get the sum of two numbers #include <iostream> using namespace std; int getSum( int a, int b) { return (a + b); } int main() { // function calling cout << getSum(5, 2) << endl; cout << getSum(7, 2) << endl; return 0; } |
7 9
What is a functor?
Functors are objects that can be treated as though they are a function or function pointer.
4.Iterators
As the name suggests, iterators are used for working on a sequence of values. They are the major feature that allows generality in STL. They reduce the complexity and execution time of the program.
Example:
C++
#include <bits/stdc++.h> using namespace std; // program to depict the use of iterators in c++ stl int main() { vector< int > arr = { 1, 2, 3, 4, 5 }; vector< int >::iterator p; cout << "The elements are : " ; for (p = arr.begin(); p < arr.end(); p++) cout << *p << " " ; return 0; } |
The elements are : 1 2 3 4 5
Std vs Stl
STD | STL |
---|---|
Std stands for standard | Stl stands for standard template library |
Std falls under the standard C++ Library | Stl is a subset of std |
All libraries fall under std. |
There are 4 categories of stl:
|
Space resolution operator is used(::) | No operator is used |
Examples: cin, cout under iostream header |
Example: sort(),lower_bound(). |
Please Login to comment...