🔴 Advanced · Lesson 57
set and map in STL
set and map in STL
What is set and map in STL?
set and map in STL
set stores unique sorted values. map stores key-value pairs and is useful for fast lookup.
set stores unique sorted values. map stores key-value pairs and is useful for fast lookup.
Level
🔴 STL, Modern C++ and Projects
🔴 STL, Modern C++ and Projects
Example File
set-map.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| set | Set used in set and map in STL programming. |
| map | Map used in set and map in STL programming. |
| key-value | Key-value used in set and map in STL programming. |
| unique | Unique used in set and map in STL programming. |
| lookup | Lookup used in set and map in STL programming. |
Syntax / Pattern
map<Key,Value> m; m[key] = value;
Example Program
#include <iostream>
#include <map>
using namespace std;
int main(){
map<string,int> marks;
marks["Aman"] = 92;
cout << marks["Aman"];
}
Expected Output
92
Program Explanation
- Aman is the key.
- 92 is the value associated with the key.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- student marks
- word frequency
- configuration data
Common Mistakes
- Using map when duplicate keys are required.
- Forgetting that set stores unique values.
Practice Tasks
- Count frequency of words using map.
- Store unique roll numbers using set.
Summary
set and map in STL is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is set and map in STL?
set and map in STL
set stores unique sorted values. map stores key-value pairs and is useful for fast lookup.
set stores unique sorted values. map stores key-value pairs and is useful for fast lookup.
Level
🔴 STL, Modern C++ and Projects
🔴 STL, Modern C++ and Projects
Example File
set-map.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| set | Set used in set and map in STL programming. |
| map | Map used in set and map in STL programming. |
| key-value | Key-value used in set and map in STL programming. |
| unique | Unique used in set and map in STL programming. |
| lookup | Lookup used in set and map in STL programming. |
Syntax / Pattern
map<Key,Value> m; m[key] = value;
Example Program
#include <iostream>
#include <map>
using namespace std;
int main(){
map<string,int> marks;
marks["Aman"] = 92;
cout << marks["Aman"];
}
Expected Output
92
Program Explanation
- Aman is the key.
- 92 is the value associated with the key.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- student marks
- word frequency
- configuration data
Common Mistakes
- Using map when duplicate keys are required.
- Forgetting that set stores unique values.
Practice Tasks
- Count frequency of words using map.
- Store unique roll numbers using set.
Summary
set and map in STL is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.