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:
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
OutputThe 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:
- Algorithms
- Functions.
- Iterators
- Containers.
|
Space resolution operator is used(::) | No operator is used |
Examples:
cin, cout under iostream header
|
Example:
sort(),lower_bound().
|
Similar Reads
std::endl vs
in C++
std::endl and \n both seem to do the same thing but there is a subtle difference between them. std::cout << std::endl inserts a new line and flushes the stream(output buffer), whereas std::cout << "\n" just inserts a new line. Therefore, std::cout << std::endl; can be said equivale
3 min read
List in C++ STL
In C++, list container implements a doubly linked list in which each element contains the address of next and previous element in the list. It stores data in non-contiguous memory, hence providing fast insertion and deletion once the position of the element is known.Example:C++#include <iostream
7 min read
Perl vs C/C++
Perl is a general purpose, high level interpreted and dynamic programming language. It was developed by Larry Wall, in 1987. There is no official acronym for the Perl, but still, the most used acronym is “Practical Extraction and Reporting Language”. Some of the programmers also
3 min read
C++ using vs Typedef
typedef keyword in C++ is used for aliasing existing data types, user-defined data types, and pointers to a more meaningful name. Typedefs allow you to give descriptive names to standard data types, which can also help you self-document your code. Mostly typedefs are used for aliasing, only if the p
2 min read
strcat() vs strncat() in C++
strcat() The strcat() function will append a copy of the source string to the end of destination string. The strcat() function takes two arguments: 1) dest 2) src It will append copy of the source string in the destination string. The terminating character at the end of dest is replaced by the first
3 min read
emplace vs insert in C++ STL
In C++, all containers (vector, stack, queue, set, map, etc) support both insert and emplace operations. Both are used to add an element in the container.The advantage of emplace is, it does in-place insertion and avoids an unnecessary copy of object. For primitive data types, it does not matter whi
1 min read
C++ vs C#
C++ and C# both are commonly used programming languages and came up with different powerful features used in different use cases. In this article, we are going to explore the common differences between these two programming languages based on their distinct features, use cases, and ecosystems.C# is
7 min read
C++ STL Quizzes
C++ Standard Template Library (STL) provides the inbuilt implementations of commonly used data structures and algorithms. It also provides other components such as iterators and functions to make the programming faster and more robust.This quiz will help you test your understanding of the key compon
3 min read
C++ STL Cheat Sheet
The C++ STL Cheat Sheet provides short and concise notes on Standard Template Library (STL) in C++. Designed for programmers that want to quickly go through key STL concepts, the STL cheatsheet covers the concepts such as vectors and other containers, iterators, functors, etc., with their syntax and
15+ min read
std::stoul and std::stoull in C++
std::stoul Convert string to unsigned integer. Parses str interpreting its content as an integral number of the specified base, which is returned as an unsigned long value. unsigned long stoul (const string& str, size_t* idx = 0, int base = 10); Parameters : str : String object with the represen
3 min read