C++
// C++ code
#include
#include
using namespace std;
int main()
{
// Declaring the PAIR1 of int and char
// IF pair is not intialized then ,
// default value of int/double is 0 and
// for string/char it is NULL
pair PAIR1;
cout << PAIR1.first << " ";
// NULL value therefore, not displayed
cout << PAIR1.second
<< endl;
// Intializing the pair during it's Declaration
pair PAIR2("GeeksForGeeks", 1.23);
cout << PAIR2.first << " ";
cout << PAIR2.second << endl;
pair PAIR3;
// Inserting Value in pair using make_pair function
PAIR3 = make_pair("GeeksForGeeks is Best", 4.56);
cout << PAIR3.first << " ";
cout << PAIR3.second << endl;
pair PAIR4;
// Inserting Value in pair using {}(curly brackets)
PAIR4 = { 4, 8 };
cout << PAIR4.first << " ";
cout << PAIR4.second << endl;
return 0;
}
C++
// C++ program to illustrate the
// function of vector in C++
#include
// Header file for vector if
// not included
#include
using namespace std;
// Function to print the vector
void print(vector vec)
{
// vec.size() gives the size
// of the vector
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
}
// Driver Code
int main()
{
// Defining a vector
vector vec;
// Put all natural numbers
// from 1 to 10 in vector
for (int i = 1; i <= 10; i++) {
vec.push_back(i);
}
cout << "Initial vector: ";
// print the vector
print(vec);
// Size of vector
cout << "Vector size: " << vec.size() << "\n";
// Check of vector is empty
if (vec.empty() == false)
cout << "Is vector is"
<< " empty: False\n";
// Popping out 10 form the vector
vec.pop_back();
cout << "Vector after popping: ";
print(vec);
// Deleting the first element
// from the vector using erase()
vec.erase(vec.begin());
cout << "Vector after erase"
<< " first element: ";
print(vec);
// Clear the vector
vec.clear();
cout << "Vector after "
<< "clearing: None ";
print(vec);
// Check if vector is empty
if (vec.empty() == true)
cout << "Is vector is"
<< " empty: True\n";
}
C++
// C++ program to illustrate the
// function of stack in C++
#include
// Header file for stack
#include
using namespace std;
// Function to print the stack
void print(stack s)
{
// Loops runs till stack
// becomes empty
while (s.empty() == false) {
// Prints the top element
cout << s.top() << " ";
// Now pops the same top element
s.pop();
}
cout << "\n";
}
// Driver Code
int main()
{
// Given char array
char array[]
= { 'G', 'E', 'E', 'K',
'S', 'F', 'O', 'R', 'G',
'E', 'E', 'K', 'S' };
// Defining stack
stack s;
// Check if stack is empty
if (s.empty() == true) {
cout << "Stack is currently Empty"
<< "\n";
}
else {
cout << "Stack is not empty"
<< "\n";
}
// Push elements in stack
for (int i = sizeof(array) / sizeof(array[0]) - 1;
i >= 0; i--) {
s.push(array[i]);
}
// Size of stack
cout << "Size of stack: "
<< s.size() << "\n";
// Content of stack
cout << "Stack initially: ";
print(s);
// Returning the top
// element of the stack
cout << "Top element: "
<< s.top() << "\n";
// Popping the top
// element in stack
s.pop();
cout << "Stack after 1"
<< "pop operation: ";
print(s);
// Now checking the top element
cout << "Top element after popping: "
<< s.top() << "\n";
// Size of stack
cout << "Size of stack"
<< "after popping: "
<< s.size() << "\n";
// Again checking if the
// stack is empty
if (s.empty() == true) {
cout << "Stack is currently Empty"
<< "\n";
}
else {
cout << "Stack is not empty"
<< "\n";
}
return 0;
}
C++
// C++ program to illustrate the
// function of vector in C++
#include
// Header file for queue
#include
using namespace std;
// Function to print the queue
void print(queue q)
{
for (int i = 0; i < q.size(); i++) {
// Printing the front element
cout << q.front() << " ";
// Popping the front element
q.pop();
}
cout << "\n";
}
// Driver Code
int main()
{
// Given array
char array[]
= { 'G', 'E', 'E', 'K', 'S' };
// Defining queue
queue q;
if (q.empty() == true) {
cout << "Queue is empty\n";
}
for (int i = 0; i < 5; i++) {
q.push(array[i]);
}
cout << "Queue Initally: ";
print(q);
// Front element
cout << "Front element: "
<< q.front() << "\n";
// Back element
cout << "Back Element: "
<< q.back() << "\n";
// Size of queue
cout << "Size of queue: "
<< q.size() << "\n";
// Empty
if (q.empty() == false) {
cout << "Queue is not empty\n";
}
return 0;
}
C++
// C++ program to illustrate the
// function of priority queue in C++
#include
// Header file for priority
// queue, both MIN and MAX
#include
using namespace std;
// Function to print the
// min priority queue
void print_min(
priority_queue, greater > q)
{
while (q.empty() == false)
{
// Print the front
// element(MINIMUM)
cout << q.top() << " ";
// Pop the minimum
q.pop();
}
cout << "\n";
}
// Function to print the
// min priority queue
void print_max(priority_queue q)
{
while (q.empty() == false)
{
// Print the front
// element(MAXIMUM)
cout << q.top() << " ";
// Pop the maximum
q.pop();
}
cout << "\n";
}
// Driver Code
int main()
{
// MIN priority queue
priority_queue max_pq;
// MAX priority_queue
priority_queue, greater > min_pq;
// Is queue empty
if (min_pq.empty() == true)
cout << "MIN priority "
<< "queue is empty\n";
if (max_pq.empty() == true)
cout << "MAX priority"
<< " queue is empty\n";
cout << "\n";
for (int i = 1; i <= 10; i++)
{
min_pq.push(i);
max_pq.push(i);
}
cout << "MIN priority queue: ";
print_min(min_pq);
cout << "MAX priority queue: ";
print_max(max_pq);
cout << "\n";
// Size
cout << "Size of min pq: " << min_pq.size() << "\n";
cout << "Size of max pq: " << max_pq.size() << "\n";
cout << "\n";
// Top element
cout << "Top of min pq: " << min_pq.top() << "\n";
cout << "Top of max pq: " << max_pq.top() << "\n";
cout << "\n";
// Pop the front element
min_pq.pop();
max_pq.pop();
// Queus after popping
cout << "MIN priority "
<< "queue after pop: ";
print_min(min_pq);
cout << "MAX priority "
<< "queue after pop: ";
print_max(max_pq);
cout << "\n";
// Size after popping
cout << "Size of min pq: " << min_pq.size() << "\n";
cout << "Size of max pq: " << max_pq.size() << "\n";
cout << "\n";
// Is queue empty
if (min_pq.empty() == false)
cout << "MIN priority "
<< " queue is not empty\n";
if (max_pq.empty() == false)
cout << "MAX priority queue"
<< " is not empty\n";
}
C++
// C++ code
#include
#include
using namespace std;
int main()
{
// Declaring the PAIR1 of int and char
// IF pair is not intialized then ,
// default value of int/double is 0
// and for string/char it is NULL
pair PAIR1;
cout << PAIR1.first << " ";
// NULL value therefore, not displayed
cout << PAIR1.second
<< endl;
// Intializing the pair during it's Declaration
pair PAIR2("GeeksForGeeks", 1.23);
cout << PAIR2.first << " ";
cout << PAIR2.second << endl;
pair PAIR3;
// Inserting Value in pair using make_pair function
PAIR3 = make_pair("GeeksForGeeks is Best", 4.56);
cout << PAIR3.first << " ";
cout << PAIR3.second << endl;
pair PAIR4;
// Inserting Value in pair using {}(curly brackets)
PAIR4 = { 4, 8 };
cout << PAIR4.first << " ";
cout << PAIR4.second << endl;
return 0;
}
C++
// C++ Code
#include
#include
#include
using namespace std;
bool ascending_secondValue(pair a,
pair b)
{
return a.second < b.second;
}
bool descending_firstValue(pair a,
pair b)
{
return a.first > b.first;
}
bool descending_secondValue(pair a,
pair b)
{
return a.second > b.second;
}
// Driver Code
int main()
{
pair PAIR1[5];
PAIR1[0] = make_pair(1, 3);
PAIR1[1] = make_pair(13, 4);
PAIR1[2] = make_pair(5, 12);
PAIR1[3] = make_pair(7, 9);
// Using {} to insert element instead
// of make_pair you can use any
PAIR1[4]
= { 11, 2 };
cout << "Sorting array in Ascending "
"order on the basis of First value - "
<< endl;
sort(PAIR1, PAIR1 + 5);
for (int i = 0; i < 5; i++) {
cout << PAIR1[i].first << " " << PAIR1[i].second
<< endl;
}
pair PAIR2[5];
PAIR2[0] = make_pair(1, 3);
PAIR2[1] = make_pair(13, 4);
PAIR2[2] = make_pair(5, 12);
PAIR2[3] = make_pair(7, 9);
PAIR2[4] = make_pair(11, 2);
cout << "Sorting array in Ascending "
" order on the basis of Second value - "
<< endl;
sort(PAIR2, PAIR2 + 5, ascending_secondValue);
for (int i = 0; i < 5; i++)
{
cout << PAIR2[i].first
<< " " << PAIR2[i].second
<< endl;
}
pair PAIR3[5];
PAIR3[0] = make_pair(1, 3);
PAIR3[1] = make_pair(13, 4);
PAIR3[2] = make_pair(5, 12);
PAIR3[3] = make_pair(7, 9);
PAIR3[4] = make_pair(11, 2);
cout << "Sorting array in Descending order on the "
"basis of First value - "
<< endl;
sort(PAIR3, PAIR3 + 5, descending_firstValue);
for (int i = 0; i < 5; i++) {
cout << PAIR3[i].first << " "
<< PAIR3[i].second
<< endl;
}
pair PAIR4[5];
PAIR4[0] = make_pair(1, 3);
PAIR4[1] = make_pair(13, 4);
PAIR4[2] = make_pair(5, 12);
PAIR4[3] = make_pair(7, 9);
PAIR4[4] = make_pair(11, 2);
cout << "Sorting array in Descending order on the "
"basis of Second value - "
<< endl;
sort(PAIR4, PAIR4 + 5, descending_secondValue);
for (int i = 0; i < 5; i++) {
cout << PAIR4[i].first << " "
<< PAIR4[i].second
<< endl;
}
return 0;
}
C++
// C++ program
#include
using namespace std;
// Header files, namespaces,
// macros as defined above
#include
#include
using namespace __gnu_pbds;
// ordered_set is just macro you can give any
// other name also
#define ordered_set \
tree, rb_tree_tag, \
tree_order_statistics_node_update>
// Driver program to test above functions
int main()
{
// Ordered set declared with name o_set
ordered_set o_set;
// insert function to insert in
// ordered set same as SET STL
o_set.insert(5);
o_set.insert(1);
o_set.insert(2);
// Finding the second smallest element
// in the set using * because
// find_by_order returns an iterator
cout << *(o_set.find_by_order(1)) << endl;
// Finding the number of elements
// strictly less than k=4
cout << o_set.order_of_key(4) << endl;
// Finding the count of elements less
// than or equal to 4 i.e. strictly less
// than 5 if integers are present
cout << o_set.order_of_key(5) << endl;
// Deleting 2 from the set if it exists
if (o_set.find(2) != o_set.end()) {
o_set.erase(o_set.find(2));
}
// Now after deleting 2 from the set
// Finding the second smallest element in the set
cout << *(o_set.find_by_order(1)) << endl;
// Finding the number of
// elements strictly less than k=4
cout << o_set.order_of_key(4) << endl;
return 0;
}
STL提供了一系列数据结构,这些数据结构在各种情况下都非常有用。许多数据结构都基于实际应用。它是容器类,算法和迭代器的库。它是一个通用库,因此其组件已参数化。
最常用的数据结构是:
- 向量
- 堆
- 队列
- 优先队列
- 放
- 列表
- 有序地图
- 无序地图
容器或容器类存储对象和数据。总共有七个标准“一流”容器类和三个容器适配器类,并且只有七个头文件可提供对这些容器或容器适配器的访问。
注意:我们只能包含一个库,即#include
- 向量:使用数组时的主要问题是我们必须指定大小。矢量克服了这一缺点。向量在内部作为动态分配的数组工作,这是我们如何在不指定向量大小的情况下添加元素的主要原因。当向量的大小等于容量时,向量的容量会增加,因此我们可以添加更多元素。
头文件:
#include
句法:
vector variable_name;
向量最常见的函数:
- push_back():用于将元素推入向量的末尾。对于更快的方法,请使用emplace_back()。
- pop_back():用于从向量中删除最后一个元素。
- size():返回向量的大小。
- clear():删除向量的所有内容。
- delete():删除指定的索引或数据。
- empty():如果向量为空,则返回布尔值True,否则返回False。
- 迭代器lower_bound(Iterator first,Iterator last,const val):lower_bound返回一个迭代器,该迭代器指向[first,last)范围内的第一个元素,其值不小于’val’。
- 迭代器upper_bound(Iterator first,Iterator last,const val):upper_bound返回一个迭代器,该迭代器指向[first,last)范围内的第一个元素,该元素的值大于‘val’。
C++
// C++ program to illustrate the
// function of vector in C++
#include
// Header file for vector if
// not included
#include
using namespace std;
// Function to print the vector
void print(vector vec)
{
// vec.size() gives the size
// of the vector
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
}
// Driver Code
int main()
{
// Defining a vector
vector vec;
// Put all natural numbers
// from 1 to 10 in vector
for (int i = 1; i <= 10; i++) {
vec.push_back(i);
}
cout << "Initial vector: ";
// print the vector
print(vec);
// Size of vector
cout << "Vector size: " << vec.size() << "\n";
// Check of vector is empty
if (vec.empty() == false)
cout << "Is vector is"
<< " empty: False\n";
// Popping out 10 form the vector
vec.pop_back();
cout << "Vector after popping: ";
print(vec);
// Deleting the first element
// from the vector using erase()
vec.erase(vec.begin());
cout << "Vector after erase"
<< " first element: ";
print(vec);
// Clear the vector
vec.clear();
cout << "Vector after "
<< "clearing: None ";
print(vec);
// Check if vector is empty
if (vec.empty() == true)
cout << "Is vector is"
<< " empty: True\n";
}
Initial vector: 1 2 3 4 5 6 7 8 9 10
Vector size: 10
Is vector is empty: False
Vector after popping: 1 2 3 4 5 6 7 8 9
Vector after erase first element: 2 3 4 5 6 7 8 9
Vector after clearing: None
Is vector is empty: True
2.堆栈:这是后进先出(LIFO)数据结构。可以使用数组,链接列表和向量来实现。使用堆栈类可以解决一些问题,例如反转元素或字符串,括号检查,打印下一个更大的元素,后缀表达式等,而不是使所有函数都可以使用其内置函数来解决。
头文件:
#include
句法:
stack
堆栈最常用的函数:
- push():用于将元素压入堆栈顶部。
- pop():删除堆栈的顶部元素,但不返回。
- top():返回堆栈的顶部元素。
- empty():返回布尔值,即,如果堆栈为空,则返回True,否则返回false。
- size():返回堆栈的大小。
C++
// C++ program to illustrate the
// function of stack in C++
#include
// Header file for stack
#include
using namespace std;
// Function to print the stack
void print(stack s)
{
// Loops runs till stack
// becomes empty
while (s.empty() == false) {
// Prints the top element
cout << s.top() << " ";
// Now pops the same top element
s.pop();
}
cout << "\n";
}
// Driver Code
int main()
{
// Given char array
char array[]
= { 'G', 'E', 'E', 'K',
'S', 'F', 'O', 'R', 'G',
'E', 'E', 'K', 'S' };
// Defining stack
stack s;
// Check if stack is empty
if (s.empty() == true) {
cout << "Stack is currently Empty"
<< "\n";
}
else {
cout << "Stack is not empty"
<< "\n";
}
// Push elements in stack
for (int i = sizeof(array) / sizeof(array[0]) - 1;
i >= 0; i--) {
s.push(array[i]);
}
// Size of stack
cout << "Size of stack: "
<< s.size() << "\n";
// Content of stack
cout << "Stack initially: ";
print(s);
// Returning the top
// element of the stack
cout << "Top element: "
<< s.top() << "\n";
// Popping the top
// element in stack
s.pop();
cout << "Stack after 1"
<< "pop operation: ";
print(s);
// Now checking the top element
cout << "Top element after popping: "
<< s.top() << "\n";
// Size of stack
cout << "Size of stack"
<< "after popping: "
<< s.size() << "\n";
// Again checking if the
// stack is empty
if (s.empty() == true) {
cout << "Stack is currently Empty"
<< "\n";
}
else {
cout << "Stack is not empty"
<< "\n";
}
return 0;
}
Stack is currently Empty
Size of stack: 13
Stack initially: G E E K S F O R G E E K S
Top element: G
Stack after 1pop operation: E E K S F O R G E E K S
Top element after popping: E
Size of stackafter popping: 12
Stack is not empty
3.Queue :这是先进先出(FIFO)数据结构。我们要求队列的原因是大量的先进先出实际应用以及数据不需要尽早处理的情况。例如,在购买演出门票的队列中,首先进入队列的人首先获得门票。可以像堆栈一样使用数组,链接列表和向量来实现它。队列的一些应用包括在树和图中的层次顺序遍历,在资源共享中等。
头文件:
#include
句法:
queue variable_name;
队列最常用的函数:
- push():用于将元素推送到队列的后面
- pop():删除队列的前部元素,但不返回。
- front():返回队列的前元素,或该行中的第一个元素。
- empty():返回布尔值,即,如果队列为空,则返回True,否则返回false
- back():返回队列的最后一个元素。
- size():返回队列的大小。
C++
// C++ program to illustrate the
// function of vector in C++
#include
// Header file for queue
#include
using namespace std;
// Function to print the queue
void print(queue q)
{
for (int i = 0; i < q.size(); i++) {
// Printing the front element
cout << q.front() << " ";
// Popping the front element
q.pop();
}
cout << "\n";
}
// Driver Code
int main()
{
// Given array
char array[]
= { 'G', 'E', 'E', 'K', 'S' };
// Defining queue
queue q;
if (q.empty() == true) {
cout << "Queue is empty\n";
}
for (int i = 0; i < 5; i++) {
q.push(array[i]);
}
cout << "Queue Initally: ";
print(q);
// Front element
cout << "Front element: "
<< q.front() << "\n";
// Back element
cout << "Back Element: "
<< q.back() << "\n";
// Size of queue
cout << "Size of queue: "
<< q.size() << "\n";
// Empty
if (q.empty() == false) {
cout << "Queue is not empty\n";
}
return 0;
}
Queue is empty
Queue Initally: G E E
Front element: G
Back Element: S
Size of queue: 5
Queue is not empty
4.Priority Queue :此数据结构类似于队列,但是先进先出的顺序由用户设置的优先级决定。主要功能包括获取最高优先级元素,插入,删除最高优先级元素或降低优先级。数据结构使用堆而不是BST,就像在BST中那样,创建树比堆更昂贵,并且堆的复杂性更好。同样,堆提供了完整的二叉树和堆顺序属性,这些属性满足Priority Queue的所有属性。优先队列有2种变化,最小堆和最大堆。
可以轻松实现复杂的问题,例如查找k个最大或最小元素,合并k个未排序的数组,Dijkstra的最短路径算法,用于压缩的霍夫曼代码,Prim的算法等。
头文件:
#include
句法:
Min Priority Queue: priority_queue variable_name;
Max Priority Queue: priority_queue variable_name;
优先级队列最常用的函数:
- push():用于将元素推送到队列中
- pop():删除队列的最高优先级元素,但不返回它。删除最大堆中具有最高优先级的元素,否则删除最小元素
- size():返回队列的大小。
- empty():返回布尔值,即,如果队列为空,则返回true,否则返回false。
- top():返回队列的顶部元素。在最大优先级队列中,它返回最大值,而在最小优先级队列中,它返回最小值。
C++
// C++ program to illustrate the
// function of priority queue in C++
#include
// Header file for priority
// queue, both MIN and MAX
#include
using namespace std;
// Function to print the
// min priority queue
void print_min(
priority_queue, greater > q)
{
while (q.empty() == false)
{
// Print the front
// element(MINIMUM)
cout << q.top() << " ";
// Pop the minimum
q.pop();
}
cout << "\n";
}
// Function to print the
// min priority queue
void print_max(priority_queue q)
{
while (q.empty() == false)
{
// Print the front
// element(MAXIMUM)
cout << q.top() << " ";
// Pop the maximum
q.pop();
}
cout << "\n";
}
// Driver Code
int main()
{
// MIN priority queue
priority_queue max_pq;
// MAX priority_queue
priority_queue, greater > min_pq;
// Is queue empty
if (min_pq.empty() == true)
cout << "MIN priority "
<< "queue is empty\n";
if (max_pq.empty() == true)
cout << "MAX priority"
<< " queue is empty\n";
cout << "\n";
for (int i = 1; i <= 10; i++)
{
min_pq.push(i);
max_pq.push(i);
}
cout << "MIN priority queue: ";
print_min(min_pq);
cout << "MAX priority queue: ";
print_max(max_pq);
cout << "\n";
// Size
cout << "Size of min pq: " << min_pq.size() << "\n";
cout << "Size of max pq: " << max_pq.size() << "\n";
cout << "\n";
// Top element
cout << "Top of min pq: " << min_pq.top() << "\n";
cout << "Top of max pq: " << max_pq.top() << "\n";
cout << "\n";
// Pop the front element
min_pq.pop();
max_pq.pop();
// Queus after popping
cout << "MIN priority "
<< "queue after pop: ";
print_min(min_pq);
cout << "MAX priority "
<< "queue after pop: ";
print_max(max_pq);
cout << "\n";
// Size after popping
cout << "Size of min pq: " << min_pq.size() << "\n";
cout << "Size of max pq: " << max_pq.size() << "\n";
cout << "\n";
// Is queue empty
if (min_pq.empty() == false)
cout << "MIN priority "
<< " queue is not empty\n";
if (max_pq.empty() == false)
cout << "MAX priority queue"
<< " is not empty\n";
}
MIN priority queue is empty
MAX priority queue is empty
MIN priority queue: 1 2 3 4 5 6 7 8 9 10
MAX priority queue: 10 9 8 7 6 5 4 3 2 1
Size of min pq: 10
Size of max pq: 10
Top of min pq: 1
Top of max pq: 10
MIN priority queue after pop: 2 3 4 5 6 7 8 9 10
MAX priority queue after pop: 9 8 7 6 5 4 3 2 1
Size of min pq: 9
Size of max pq: 9
MIN priority queue is not empty
MAX priority queue is not empty
4.Set :集合是关联容器,其中每个元素都是唯一的。元素一旦插入集合便无法修改。集合将忽略重复的值,并且所有元素均按排序顺序存储。当需要对传入元素进行排序并且不需要修改时,此数据结构特别有用。
这些集合可以按两个顺序(递增顺序或递减顺序)存储元素。
头文件:
#include
句法:
Increasing order: set variable_name;
Decreasing order :set > variable_name;
设置最常用的函数:
- insert():此函数用于在Set中插入一个新元素。
- begin():此函数将迭代器返回到集合中的第一个元素。
- end():它将迭代器返回到集合中最后一个元素之后的理论元素。
- size():返回集合的总大小。
- find():如果存在,它将返回迭代器到搜索到的元素。如果没有,它将给出一个迭代器。
- count():返回集合中出现的次数。如果存在,则为1,否则为0。
- empty():返回布尔值,即,如果为空则返回true,否则返回false。
5,列表:列表以非连续方式存储数据,列表中的元素可以分散在不同的内存块中,由于必须执行从已知索引到该特定索引的遍历,因此访问任何特定索引的成本都很高比矢量慢。零大小的列表也有效。
头文件:
#include
句法:
list variable_name;
最常用的函数列表:
- push_front(element):在列表的开头插入一个新元素’element’。
- push_back(element):在列表末尾插入一个新元素’element’。
- pop_front():删除列表的第一个元素。
- pop_back():删除列表的最后一个元素。
- front():返回列表中第一个元素的值。
- back():返回列表中最后一个元素的值。
- empty():返回布尔值,即,如果为空则返回true,否则返回false。
6.Unordered Maps :假设您有一个铅笔盒和一些笔。您将一些笔放在框中,该笔是随机的,与有序地图不同,不是有序的,但是您可以访问任何笔并使用它。相同的是无序映射,元素是随机存储的,但是您可以随时访问任何顺序。主要区别在于有序映射和无序映射之间是编译器存储它们的方式。它有两个参数,第一个称为KEY ,另一个称为Value 。键值映射到该值,并且始终是唯一的。
头文件:
#include
句法:
unordered_map variable name;
无序图最常用的函数:
- count():返回布尔值,即,如果存在密钥通过,则返回1,否则返回false。
- delete(key):返回传递的密钥。
- clear():删除整个地图。
- size():返回地图的大小。
7.Ordered Maps:假设您的房间里有一个新书架和一些书。您可以一本又一本地排列书籍,但是在排列任意数量的书籍之后,您可以按任何顺序访问排列的书籍,并且在阅读书籍时将其放在同一位置。这是地图的示例。您可以按顺序一个接一个地填充值,并且该顺序始终保持不变,但是您可以随时访问任何元素。该数据结构也使用动态数组来实现。像无序映射一样,它有两个参数,第一个称为KEY ,另一个称为Value 。键值映射到该值,并且始终是唯一的。
头文件:
#include
句法:
ordered_map variable name;
有序图最常用的函数:
- count():返回布尔值,即如果存在密钥通过,则返回1,否则返回false。
- delete(key):返回传递的密钥。
- clear():删除整个地图。
- size():返回地图的大小。
其他有用的STL是–
1.)Pair: Pair容器是在
头文件:
#include
句法 :
pair (data_type1, data_type2) Pair_name;
Pair最常见的函数:
- make_pair() :此模板函数允许创建值对,而无需显式编写类型。
以下是实现上述函数的代码:–
C++
// C++ code
#include
#include
using namespace std;
int main()
{
// Declaring the PAIR1 of int and char
// IF pair is not intialized then ,
// default value of int/double is 0
// and for string/char it is NULL
pair PAIR1;
cout << PAIR1.first << " ";
// NULL value therefore, not displayed
cout << PAIR1.second
<< endl;
// Intializing the pair during it's Declaration
pair PAIR2("GeeksForGeeks", 1.23);
cout << PAIR2.first << " ";
cout << PAIR2.second << endl;
pair PAIR3;
// Inserting Value in pair using make_pair function
PAIR3 = make_pair("GeeksForGeeks is Best", 4.56);
cout << PAIR3.first << " ";
cout << PAIR3.second << endl;
pair PAIR4;
// Inserting Value in pair using {}(curly brackets)
PAIR4 = { 4, 8 };
cout << PAIR4.first << " ";
cout << PAIR4.second << endl;
return 0;
}
如果有对的阵列,并且使用内置的排序函数然后,默认排序根据第一值中的每个元素。为了按降序排序的阵列的基础上(即obj.first)对数组传递函数您想对数组进行排序。
下面的代码将显示如何对对数组进行排序:–
C++
// C++ Code
#include
#include
#include
using namespace std;
bool ascending_secondValue(pair a,
pair b)
{
return a.second < b.second;
}
bool descending_firstValue(pair a,
pair b)
{
return a.first > b.first;
}
bool descending_secondValue(pair a,
pair b)
{
return a.second > b.second;
}
// Driver Code
int main()
{
pair PAIR1[5];
PAIR1[0] = make_pair(1, 3);
PAIR1[1] = make_pair(13, 4);
PAIR1[2] = make_pair(5, 12);
PAIR1[3] = make_pair(7, 9);
// Using {} to insert element instead
// of make_pair you can use any
PAIR1[4]
= { 11, 2 };
cout << "Sorting array in Ascending "
"order on the basis of First value - "
<< endl;
sort(PAIR1, PAIR1 + 5);
for (int i = 0; i < 5; i++) {
cout << PAIR1[i].first << " " << PAIR1[i].second
<< endl;
}
pair PAIR2[5];
PAIR2[0] = make_pair(1, 3);
PAIR2[1] = make_pair(13, 4);
PAIR2[2] = make_pair(5, 12);
PAIR2[3] = make_pair(7, 9);
PAIR2[4] = make_pair(11, 2);
cout << "Sorting array in Ascending "
" order on the basis of Second value - "
<< endl;
sort(PAIR2, PAIR2 + 5, ascending_secondValue);
for (int i = 0; i < 5; i++)
{
cout << PAIR2[i].first
<< " " << PAIR2[i].second
<< endl;
}
pair PAIR3[5];
PAIR3[0] = make_pair(1, 3);
PAIR3[1] = make_pair(13, 4);
PAIR3[2] = make_pair(5, 12);
PAIR3[3] = make_pair(7, 9);
PAIR3[4] = make_pair(11, 2);
cout << "Sorting array in Descending order on the "
"basis of First value - "
<< endl;
sort(PAIR3, PAIR3 + 5, descending_firstValue);
for (int i = 0; i < 5; i++) {
cout << PAIR3[i].first << " "
<< PAIR3[i].second
<< endl;
}
pair PAIR4[5];
PAIR4[0] = make_pair(1, 3);
PAIR4[1] = make_pair(13, 4);
PAIR4[2] = make_pair(5, 12);
PAIR4[3] = make_pair(7, 9);
PAIR4[4] = make_pair(11, 2);
cout << "Sorting array in Descending order on the "
"basis of Second value - "
<< endl;
sort(PAIR4, PAIR4 + 5, descending_secondValue);
for (int i = 0; i < 5; i++) {
cout << PAIR4[i].first << " "
<< PAIR4[i].second
<< endl;
}
return 0;
}
有序集:有序集是g ++中基于策略的数据结构,可将唯一元素保持在已排序的顺序中。它以log(n)复杂度执行由STL中的set数据结构执行的所有操作,还以log(n)复杂度执行两个附加操作。
- order_of_key(k) :严格小于k的项目数。
- find_by_order(k) :集合中的第K个元素(从零开始计数)。
头文件和名称空间:
#include
#include
using namespace __gnu_pbds;
有序集合实现所需的必要结构是:
tree < int , null_type , less , rb_tree_tag , tree_order_statistics_node_update >
您可以在此处详细了解它们。
除集合外,有序集合中的其他功能是–
- find_by_order(k):在O(log n)时间中返回集合中第k个元素(从零开始计数)的迭代器。要找到第一个元素k必须为零。
让我们假设我们有一个集合s:{1、5、6、17、88},然后:
*(s.find_by_order(2)):集合中的第三个元素,即6
*(s.find_by_order(4)):集合中的第5个元素,即88
2. order_of_key(k) :它返回在O(log n)时间内严格小于我们的项目k的项目数。
让我们假设我们有一个集合s:{1、5、6、17、88},然后:
s.order_of_key(6):严格小于6的元素数为2。
s.order_of_key(25):严格小于25的元素数为4。
NOTE : ordered_set is used here as a macro given to
tree.
Therefore it can be given any name as macro other than ordered_set
but generally in the world of competitive programming it is commonly referred as ordered set
as it is a set with additional operations.
C++
// C++ program
#include
using namespace std;
// Header files, namespaces,
// macros as defined above
#include
#include
using namespace __gnu_pbds;
// ordered_set is just macro you can give any
// other name also
#define ordered_set \
tree, rb_tree_tag, \
tree_order_statistics_node_update>
// Driver program to test above functions
int main()
{
// Ordered set declared with name o_set
ordered_set o_set;
// insert function to insert in
// ordered set same as SET STL
o_set.insert(5);
o_set.insert(1);
o_set.insert(2);
// Finding the second smallest element
// in the set using * because
// find_by_order returns an iterator
cout << *(o_set.find_by_order(1)) << endl;
// Finding the number of elements
// strictly less than k=4
cout << o_set.order_of_key(4) << endl;
// Finding the count of elements less
// than or equal to 4 i.e. strictly less
// than 5 if integers are present
cout << o_set.order_of_key(5) << endl;
// Deleting 2 from the set if it exists
if (o_set.find(2) != o_set.end()) {
o_set.erase(o_set.find(2));
}
// Now after deleting 2 from the set
// Finding the second smallest element in the set
cout << *(o_set.find_by_order(1)) << endl;
// Finding the number of
// elements strictly less than k=4
cout << o_set.order_of_key(4) << endl;
return 0;
}
2
2
2
5
1