The most common container types in C++ STL are Vector, List, Deque, Set, Map, Stack and Queue. These containers provide solutions for different data storage needs, such as dynamic arrays, doubly linked lists, and key- and value-based associative containers. In practice, we can use STL containers to organize and access data efficiently, such as storing student grades.
Common types in C++ STL containers
The Standard Template Library (STL) is a set of common containers provided in the C++ standard library and algorithms. These containers are used to store and organize data, and STL contains various container types to meet different data storage needs.
The most common STL container types include:
Practical case:
Consider a program that needs to store student scores. We can use STL containers to manage and access data efficiently.
#include <iostream> #include <vector> #include <map> using namespace std; int main() { // 创建一个学生成绩的vector vector<int> grades; // 加入一些成绩 grades.push_back(90); grades.push_back(85); grades.push_back(75); // 创建一个学生姓名到成绩的map map<string, int> student_grades; // 加入一些学生姓名和成绩 student_grades["John"] = 90; student_grades["Jane"] = 85; student_grades["Jim"] = 75; // 访问学生成绩 cout << "John's grade: " << student_grades["John"] << endl; // 遍历vector中的成绩 for (int grade : grades) { cout << grade << " "; } cout << endl; return 0; }
The above is the detailed content of What are the common types in C++ STL containers?. For more information, please follow other related articles on the PHP Chinese website!