Home  >  Article  >  Backend Development  >  How to implement dynamic resizing of data structures using C++ STL?

How to implement dynamic resizing of data structures using C++ STL?

王林
王林Original
2024-06-04 18:05:00756browse

Yes, dynamic resizing of data structures is possible using C++ STL containers. Containers can automatically increase or decrease in size without manual allocation of memory. Specific steps: Use std::vector to create a dynamic array. Use std::deque to create a deque. Use std::list to create a linked list.

如何使用 C++ STL 实现数据结构的动态大小调整?

How to use C++ STL to implement dynamic resizing of data structures?

The C++ Standard Template Library (STL) provides a series of powerful data structure containers to help us efficiently store and manage data in our programs. A key feature of these containers is the ability to dynamically adjust their size to accommodate changes in data volume without the need to manually reallocate memory.

Use std::vector to implement dynamic arrays

std::vector is a dynamic array container that will automatically increase or decrease when needed reduce its capacity.

#include <vector>

std::vector<int> myVector;

// 添加元素
myVector.push_back(1);
myVector.push_back(2);

// 访问元素
std::cout << myVector[0] << std::endl; // 输出:1

// 动态调整大小
myVector.pop_back(); // 删除最后一个元素
myVector.resize(5, 0); // 调整大小为 5,并用 0 填充新元素

Use std::deque to implement a double-ended queue

std::deque is a double-ended queue container that allows the queue to Efficiently add and remove elements from the head or tail.

#include <deque>

std::deque<int> myDeque;

// 添加元素
myDeque.push_front(1); // 在头部添加元素
myDeque.push_back(2); // 在尾部添加元素

// 访问元素
std::cout << myDeque.front() << std::endl; // 输出:1

// 动态调整大小
myDeque.pop_front(); // 删除头部元素
myDeque.resize(5, 0); // 调整大小为 5,并用 0 填充新元素

Use std::list to implement linked list

std::list is a doubly linked list container, which can be used in O(1) time Inserting and deleting elements within complexity.

#include <list>

std::list<int> myList;

// 添加元素
myList.push_front(1);
myList.push_back(2);

// 访问元素
auto it = myList.begin();
std::cout << *it << std::endl; // 输出:1

// 动态调整大小
myList.pop_back(); // 删除尾部元素
myList.resize(5, 0); // 调整大小为 5,并用 0 填充新元素

Practical case: Dynamically resized array

Suppose we have a program that needs to process an uncertain number of input values. We can use std::vector to create a dynamically resized array to store these inputs.

#include <vector>
#include <iostream>

int main() {
  std::vector<int> inputValues;

  // 读取输入值并添加到数组中
  int value;
  while (std::cin >> value) {
    inputValues.push_back(value);
  }

  // 处理输入值中的数据......

  return 0;
}

By using the dynamic resizing feature of STL, we can write concise and efficient C++ programs that easily meet the scalability needs of the data structures in the program.

The above is the detailed content of How to implement dynamic resizing of data structures using C++ STL?. 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