set and map in STL
Ordered Associative Container Model
set और map key द्वारा data retrieve करते और comparator से order maintain करते हैं। Default std::less normally ascending order देता है।
| Container | Data | Keys |
|---|---|---|
| set | Key | Unique |
| multiset | Key | Equivalent allowed |
| map | pair<const Key,T> | Unique |
| multimap | Key-value | Equivalent allowed |
Core search/insert/erase O(log n) हैं। Standard specific tree mandate नहीं करता। vector vs map decision guide देखें।
std::set: Unique Ordered Values
std::set<int> rolls{104,101,104,103};
for(int roll:rolls) std::cout<<roll<<' ';
auto [pos,inserted]=rolls.insert(102);Duplicate 104 insert नहीं हुआ। Set iterator key को const access देता है; in-place change ordering तोड़ सकता है। Key बदलने के लिए erase/reinsert करें। Repeated equivalent values चाहिए तो multiset लें।
std::map: Unique Key और Value
std::map<std::string,int> marks{
{"Sara",91},{"Aman",78}};
marks.insert_or_assign("Aman",83);
marks.try_emplace("Kabir",84);
for(const auto& [name,mark]:marks)
std::cout<<name<<':'<<mark<<' ';insert_or_assign insert या replace करता है; try_emplace missing key पर ही construct करता है। Iterator में key const लेकिन mapped value modifiable है।
Lookup और Access
| Operation | Result | Insert? |
|---|---|---|
| find | Iterator/end | No |
| contains C++20 | bool | No |
| count | 0/1 | No |
| at | Value/exception | No |
| operator[] | Mapped reference | Missing पर yes |
| lower_bound | Not-before key | No |
if(auto it=marks.find("Sara");it!=marks.end())
std::cout<<it->second;
if(!marks.contains("Riya")) std::cout<<"Not found";Comparators और Key Rules
Comparator strict weak ordering दे। दो keys equivalent हैं जब कोई भी दूसरे से पहले compare न हो; यह जरूरी नहीं कि == जैसा हो।
struct ByLengthThenText{
bool operator()(const std::string& a,
const std::string& b)const{
if(a.size()!=b.size()) return a.size()<b.size();
return a<b;
}
};<=, random result, time या mutable state comparator में गलत हैं। Stored keys के दौरान result consistent रहे।
Insertion, Erasure और Iterator Safety
marks.emplace("Riya",86);
marks.erase("Aman");
for(auto it=marks.begin();it!=marks.end();){
if(it->second<40) it=marks.erase(it);
else ++it;
}- Insert/emplace existing handles invalidate नहीं करते।
- Erase केवल erased element handles invalidate करता है।
- erase(iterator) next iterator देता है।
- end को dereference और erased iterator reuse न करें।
unordered_map average O(1) lookup दे सकता है लेकिन sorted order नहीं और worst O(n) संभव है।
Complete Word-Frequency Lab
std::vector<std::string> words{
"code","learn","code","cpp","learn","code"};
std::set<std::string> unique_words;
std::map<std::string,int> frequency;
for(const std::string& word:words){
unique_words.insert(word);
++frequency[word];
}यहां [] intentional है क्योंकि missing count zero से शुरू होकर increment होना चाहिए। दोनों containers lexical key order में iterate करते हैं।
Mistakes, Practice और Review
| Mistake | Correction |
|---|---|
| Insertion order expect | Comparator order |
| Read lookup में [] | find/contains/at |
| Duplicate keys expect | Multi-container/model |
| Comparator <= | Strict weak ordering |
| Key in-place change | Erase/reinsert |
| Erased iterator use | Returned next iterator |
Practice: phone book, word counter, unique roll register, lower_bound score query और custom year/roll key बनाएं।
- Set या relation?
- Unique/repeated keys?
- Comparator valid?
- Lookup inserts?
- Required order?
- Erased handles discarded?
Authoritative संदर्भ
Order, equivalence, complexity और iterator rules current draft से verified हैं। STL architecture भी पढ़ें।
