// map::get_allocator
#include <iostream>
#include <map>
#include<string>
using namespace std;
int main ()
{
int psize;
map<char,int> mymap;
pair<const char,int>* p;
// allocate an array of 5 elements using mymap's allocator:
p=mymap.get_allocator().allocate(5);
// assign some values to array
psize = (int) sizeof(map<char,int>::value_type)*5;
cout << "The allocated array has a size of " << psize << " bytes.\n";
mymap.get_allocator().deallocate(p,5);
map<string,int> map1;
pair<map<string,int>::iterator,bool> insert_pair;
map1.insert(pair<string,int>("s1",1));
map1.insert(pair<string,int>("s2",2));
map1.insert(pair<string,int>("s3",3));
map<string,int>::iterator iter;
iter=map1.begin();
while(iter!=map1.end())
{
cout<<" "<<iter->first<<" "<<iter->second<<endl;
iter++;
}
map1.insert(map<string,int>::value_type("s4",4));
map1.insert(map<string,int>::value_type("s5",5));
map1["s6"]=6;
cout<<"after insert: "<<endl;
iter=map1.begin();
while(iter!=map1.end())
{
cout<<" "<<iter->first<<" "<<iter->second<<endl;
iter++;
}
insert_pair=map1.insert(pair<string,int>("s6",6));
if (insert_pair.second==true)
{
//insert_pair->second 不能通过
cout<<"insert successfully!"<<endl;
}
else
{
cout<<"insert failure"<<endl;
}
iter=map1.find("s1");
if (iter!=map1.end())
{
cout<<"find the value is "<<iter->second<<endl;
}
else
{
cout<<"do not find"<<endl;
}
return 0;
}
/***
//member functions
map<Key,T>::iterator it
(*it)->first //the key value(of type key)
(*it)->second //the mapped value(of type T)
(*it) //the element value (of type pair <const Key,T>)
it->first //the same as (*it)->first
it->second //the same as (*it)->second
(constructor)
(destructor)
operator =
//iterators
begin()
end()
rbegin()
rend()
//capacity
empity()
size()
max_size()
//elements access
operator []
//modifiers
insert()
erase()
swap()
clear()
//observers
key_comp()
value_comp()
//operations
find()
count()
lower_bound()
upper_bound()
equal_range()
//allcoator
get_allocator()
***/
map--初步学习STL
最新推荐文章于 2023-06-01 08:30:00 发布

513

被折叠的 条评论
为什么被折叠?



