YouTip
Home
JavaScript
PHP
Python3
HTML
C#
Python
Java
PyTorch
Linux
C
jQuery
CSS
XML
jQuery UI
Bootstrap
C++
Angular
HTML DOM
Redis
Web Building
Home
>
C++
>
Cpp Libs Map
Cpp Libs Map
π 2026-06-23 | π C++
In C++, `
` is part of the Standard Template Library (STL), providing an associative container for storing key-value pairs. The elements in the `map` container are automatically sorted by key, making it ideal for scenarios requiring fast lookup and ordered data. ### Definition and Features * **Key-Value Pairs**: `map` stores key-value pairs where each key is unique. * **Sorting**: Elements in `map` are automatically sorted by key, typically in ascending order. * **Uniqueness**: Each key can only appear once in the `map`. * **Bidirectional Iterators**: `map` provides bidirectional iterators that can traverse elements forward and backward. ### Basic Syntax Include header file: #include
Declare a map container: std::map myMap; * `key_type` is the type of the key. * `value_type` is the type of the value. Insert elements: myMap = value; Access elements: value = myMap; Traverse map: for (std::map::iterator it = myMap.begin(); it != myMap.end(); ++it) { std::cout <first < " <second << std::endl;} For C++11 and above, the traversal can be simplified with a range-based for loop, making the code more concise: for (auto &p : m) { std::cout << p.first << " : " << p.second << std::endl;} ## Example Below is a simple example using `map`. We will create a `map` to store employee names and their ages, and traverse the map to print each employee's name and age. ## Example #include #include
#include int main(){ // Create a map container to store employee names and ages std::map employees; // Insert employee information employees=30; employees=25; employees=35; // Traverse the map and print employee information for(std::map::iterator it = employees.begin(); it != employees.end();++it){ std::cout<first <<" is "<second <<" years old."<< std::endl; } return 0; } Output: Alice is 30 years old.Bob is 25 years old.Charlie is 35 years old. ### Advanced Usage Check if a key exists: if (myMap.find(key) != myMap.end()) { // key exists} Remove elements: myMap.erase(key); Clear the map: myMap.clear(); Get the size of the map: size_t size = myMap.size(); Other methods: myMap.empty(); // Check if empty myMap.count("Bob"); // Check if key exists (returns 0 or 1) Custom sorting, default is ascending order, you can use std::greater or a custom comparison function: std::map<int, std::string, std::greater> m; // Descending order Using a custom comparison function: ## Example #include
#include #include bool myCompare(const std::string& a, const std::string& b){ return a < b; } int main(){ std::map<std::string, int, std::function> myMap(myCompare); // Other operations... return 0; } `map` is a very useful container in C++ STL, especially suitable for scenarios requiring fast lookup and ordered data. ## Example #include #include
#include int main(){ std::map scores; // Insert scores=90; scores=85; scores.insert({"Charlie", 92}); // Traverse for(auto&p : scores){ std::cout<< p.first< "<< p.second<< std::endl; } // Find auto it = scores.find("Bob"); if(it != scores.end()){ std::cout<<"Bob's score: "<second << std::endl; } // Remove scores.erase("Alice"); std::cout<<"Size: "<< scores.size()< 90Bob => 85Charlie => 92Bob's score: 85 Size: 2
β Cpp Libs Bitset
Cpp Libs Set β
π Categories
β‘ JavaScript
(1589)
π PHP
(872)
π Python3
(810)
π HTML
(691)
βοΈ C#
(650)
π Python
(594)
β Java
(552)
βοΈ PyTorch
(534)
π§ Linux
(472)
βοΈ C
(432)
π¦ jQuery
(406)
π¨ CSS
(377)
π XML
(259)
π¦ jQuery UI
(231)
π― Bootstrap
(220)
βοΈ C++
(215)
π °οΈ Angular
(205)
π HTML DOM
(201)
π΄ Redis
(188)
π Web Building
(142)
π Vue.js
(141)
π R
(131)
πΌ Pandas
(124)
ποΈ SQL
(105)
βοΈ Docker
(86)
βοΈ TypeScript
(73)
βοΈ Highcharts
(70)
π AI Agent
(70)
βοΈ React
(68)
π Node.js
(65)
βοΈ Machine Learning
(60)
π Git
(59)
π΅ Go
(58)
π Markdown
(58)
π’ NumPy
(55)
π§ͺ Flask
(54)
βοΈ Scala
(53)
ποΈ SQLite
(52)
π JSTL
(52)
βοΈ VS Code
(51)
π MongoDB
(49)
π Perl
(48)
π Ruby
(47)
π Matplotlib
(47)
βοΈ Uncategorized
(46)
π Swift
(46)
ποΈ PostgreSQL
(46)
βοΈ Data Structures
(46)
π Playwright
(46)
π iOS
(45)
ποΈ MySQL
(44)
βοΈ LangChain
(43)
π FastAPI
(40)
βοΈ Ionic
(38)
π Design Patterns
(37)
βοΈ Eclipse
(37)
π¨ CSS3
(34)
π Lua
(34)
βοΈ Codex
(34)
πΈ Django
(32)
βοΈ OpenCV
(32)
π Rust
(31)
π JSP
(31)
βοΈ Claude Code
(31)
π Pillow
(30)
βοΈ OpenCode
(28)
π AI Skills
(27)
π Flutter
(26)
π Maven
(26)
π¨ Tailwind CSS
(25)
π§ TensorFlow
(25)
π Servlet
(24)
π Dart
(23)
π Assembly
(23)
βοΈ Memcached
(22)
βοΈ SVG
(22)
βοΈ Electron
(22)
π NLP
(22)
π Regex
(21)
π Android
(20)
π£ Kotlin
(19)
π Julia
(19)
π SOAP
(17)
π Selenium
(17)
π PowerShell
(17)
π Sass
(16)
π HTTP
(16)
π Zig
(15)
π AI
(15)
π AJAX
(14)
π Swagger
(14)
βοΈ Scikit-learn
(13)
βοΈ ECharts
(13)
βοΈ Chart.js
(13)
βοΈ Cursor
(13)
βοΈ SciPy
(12)
π RDF
(12)
π Ollama
(12)
π Next.js
(12)
π Plotly Dash
(12)
π JSON
(11)
π RESTful API
(11)
π WSDL
(9)
βοΈ CMake
(8)
π Firebug
(7)
π Nginx
(6)
βΈοΈ Kubernetes
(6)
π Jupyter
(6)
π LaTeX
(4)
π UniApp
(4)
ποΈ SQL Server
(1)