🔴 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.
Level
🔴 STL, Modern C++ and Projects
Example File
set-map.cpp
Main Focus
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

TermMeaning / Use
setSet used in set and map in STL programming.
mapMap used in set and map in STL programming.
key-valueKey-value used in set and map in STL programming.
uniqueUnique used in set and map in STL programming.
lookupLookup 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

  1. Count frequency of words using map.
  2. 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.
Level
🔴 STL, Modern C++ and Projects
Example File
set-map.cpp
Main Focus
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

TermMeaning / Use
setSet used in set and map in STL programming.
mapMap used in set and map in STL programming.
key-valueKey-value used in set and map in STL programming.
uniqueUnique used in set and map in STL programming.
lookupLookup 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

  1. Count frequency of words using map.
  2. 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.

← Back to C++ Tutorial