Home >Backend Development >C++ >How to use C++ STL to extend the functionality of the C++ language?
C++ STL provides containers, algorithms and functions for C++, enhancing its functionality: Containers: Objects that store data, including sequential containers and associative containers. Algorithms: Functions that manipulate data, including sorting, searching, and other algorithms. Functions: Other useful functions such as math, character manipulation, and random functions.
The C++ Standard Template Library (STL) is a powerful library that provides a wide range of functions for the C++ language. Containers, algorithms and functions. It enables developers to write code in a clean and efficient manner.
Container is an object that stores data. STL provides the following containers:
Algorithm is a function that operates on data. STL provides the following algorithms:
STL also provides many other useful functions, such as :
Use vector to store a list of integers
#include <iostream> #include <vector> int main() { // 创建一个 vector std::vector<int> numbers = {1, 2, 3, 4, 5}; // 打印 vector 中的元素 for (auto n : numbers) { std::cout << n << " "; } std::cout << std::endl; // 使用 STL 函数对 vector 进行排序 std::sort(numbers.begin(), numbers.end()); // 打印排序后的 vector for (auto n : numbers) { std::cout << n << " "; } std::cout << std::endl; return 0; }
Use map to store word count
#include <iostream> #include <map> int main() { // 创建一个 map std::map<std::string, int> wordCounts; // 往 map 中添加元素 wordCounts["hello"]++; wordCounts["world"]++; wordCounts["this"]++; // 打印 map 中的元素 for (auto pair : wordCounts) { std::cout << pair.first << " appears " << pair.second << " times" << std::endl; } return 0; }
The above is the detailed content of How to use C++ STL to extend the functionality of the C++ language?. For more information, please follow other related articles on the PHP Chinese website!