Home  >  Article  >  Backend Development  >  What are the common data structures in C++ function libraries?

What are the common data structures in C++ function libraries?

WBOY
WBOYOriginal
2024-04-18 22:09:01398browse

C standard function library provides the following commonly used data structures: Array: a continuous memory block, accessing elements through indexes. Vector: A dynamically sized array that automatically grows/shrinks, providing efficient insertion/deletion/random access. Linked list: A linear data structure in which elements are stored in dynamically allocated nodes, each node containing data and a pointer to the next node. Stack: A last-in-first-out (LIFO) data structure with elements added to the top via push() and removed via pop(). Queue: A first-in-first-out (FIFO) data structure with elements added to the end via enqueque() and deleted via dequeue().

C++ 函数库中有哪些常见的数据结构?

Common data structures in the C function library

In the C standard function library, many are provided for efficient management and common data structures for processing data. Understanding these data structures is crucial as it helps you organize and retrieve data efficiently in programming.

1. Array

An array is a contiguous memory block used to store data elements of the same data type. It accesses elements by index, starting from 0. The array is declared and initialized as follows:

int arr[5] = {1, 2, 3, 4, 5};

2. Vector

A vector is a dynamically sized array that can automatically grow and shrink to accommodate elements. It provides efficient insertion, deletion and random access operations. The declaration of a vector is as follows:

vector<int> v = {1, 2, 3, 4, 5};

3. Linked List

A linked list is a linear data structure in which data elements are stored in dynamically allocated memory blocks called nodes. . Each node contains data and a pointer to the next node. Linked lists are used to create flexible and memory efficient data structures.

struct Node {
  int data;
  Node* next;
};

Node* head = new Node{1, nullptr};
head->next = new Node{2, nullptr};

4. Stack

The stack is a last-in-first-out (LIFO) data structure. Elements are added to the top of the stack using the push() operation and removed from the top of the stack using the pop() operation. The stack is used to manage function calls and local variables.

stack<int> s;
s.push(1);
s.push(2);
cout << s.top() << endl; // 2
s.pop();

5. Queue

A queue is a first-in, first-out (FIFO) data structure. Elements are added to the tail of the queue using the enqueque() operation and removed from the head of the queue using the dequeue() operation. Queues are used to manage the queuing of processes and tasks.

queue<int> q;
q.push(1);
q.push(2);
cout << q.front() << endl; // 1
q.pop();

Practical case: using vectors to store student scores

#include <vector>

int main() {
  // 创建一个向量来存储学生成绩
  vector<double> grades;

  // 添加学生成绩
  grades.push_back(90.5);
  grades.push_back(85.0);
  grades.push_back(78.2);

  // 计算平均成绩
  double sum = 0;
  for (double grade : grades) {
    sum += grade;
  }
  double average = sum / grades.size();

  // 输出平均成绩
  cout << "平均成绩:" << average << endl;

  return 0;
}

The above is the detailed content of What are the common data structures in C++ function libraries?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn