Files
Data-Structure/STL/STL-map/map.cpp
e2hang 6ca2ae6356 STL
2025-08-13 14:53:22 +08:00

34 lines
869 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main(int argc, char* argv[]){
auto cmp = [](auto a, auto b){
return a < b;
};
//与set类似但是是以std::pair的形式返回的记得写first,second
map<int, string> a;
map<int, string, decltype(cmp)> b(cmp);
map<int, string> c;
b.insert({1,"111"});
b.insert({2,"222"});
b.insert({5,"555"});
b.insert(make_pair(3,"333"));
b.emplace(make_pair(4,"444"));
cout << b.count(3) << endl;
//没有.at()和a[i]树结构不能直接访问第i个可以it++
for(auto it = b.begin(); it != b.end(); it++){
cout << it->first << ", " << it->second << " ";
}
cout << endl;
cout << b.find(3)->first << ", "<< b.find(3)->second << endl;
cout << b.upper_bound(4)->first << ", " << b.upper_bound(4)->second << endl;
return 0;
}
/*
1
1, 111 2, 222 3, 333 4, 444 5, 555
3, 333
5, 555
*/