STL map的使用(二)
来源:互联网 发布:3dmax mac中文破解版 编辑:程序博客网 时间:2024/06/03 14:42
在学习c++STL中的string,在这里做个笔记,以供自己以后翻阅和初学者参考,这里接STL map的使用(一)。
4. map::empty
功能:测试一个map容器是否为空。语法:bool empty() const;说明:empty函数用于测试一个map容器是否为空。函数返回值:当容器map为空时,返回true,否则返回false。
示例:
/***************************************************程序编号: 4程序功能说明:创建一个map容器,打印是否为空的信息。***************************************************/#include <map>#include <iostream>using namespace std;int main(){map<int,char> ctr;if (ctr.empty())//map 容器为空时{cout<<"The container is empty."<<endl;}else//map 容器为非空时{cout<<"The container is empty."<<i<<endl;}return 0;}运行结果:
运行结果如图4所示:
5. map::end
功能:返回最后一个元素后面的定位器语法:const_iterator end() const;iterator end();
说明:end 函数用于测试一个定位器是否已达到它的尾部。函数返回值:返回一个指向最后一个元素后面双向定位器地址。当map容器为空时,结果没定义。
示例:
/***************************************************程序编号: 5程序功能说明:先初始化一个map,再打印其中的内容,最后显示出最后一个元素的值。***************************************************/#include <map>#include <iostream>using namespace std;int print(map<int,int> c){map<int,int>:: const_iterator cp;for(cp=c.begin();cp!=c.end();cp++){//让cp从c的开始到结束打印cp对应的值cout<<cp->second<<"";}return 0;}int main(){map<int,char> ctr;map<int,int>::const_iterator cp;int i;for(i=0;i<3;i++){ctr.insert(pair<int,int>(i,i));//给ctr赋值}cout<<"The ctr is: ";print(ctr);//调用子程序来打印ctr的内容cout<<endl<<"The last element is: ";cp=ctr.end();//让cp指向最后一个元素的位置cp--;cout<<cp->second<<endl;//显示最后一个元素的值return 0;}
运行结果:
运行结果如图五所示。
6. map::equal_range
功能:返回一对定位器,它们分别指向第一个大于或等于给定的关键字的元素和第一个比给定的关键字大的元素。语法:pair<const_iterator, const_iterator> equal_range(const Key& _key
) const;
pair<iterator, iterator> equal_range(
const Key& _Key
) const;说明:_Key 是一个用于排序的关键字。函数返回值:返回一对定位器。
要从第一个定位器中取得数据,可用pr.first。
从第二个定位器中取得数据,则用pr.second。
示例:
/***************************************************程序编号: 6程序功能说明:先初始化一个map,再打印其中的内容,最后打印出关键字>=2或>2的元素。***************************************************/#include <map>#include <iostream>using namespace std;int print_one_item(map<int,int>::const_iterator cp)//用于打印map的一个元素{cout<<" ("<<cp->first<<", "<<cp->second<<") ";return 0;}int print(map<int,int> c)//用于打印一个map{map<int,int>::const_iterator cp;//让cp从c的开始到结束打印cp对应的值for(cp=c.begin();cp!=c.end();cp++){print_one_item(cp);//调用子程序来打印一个元素return 0;}}int main(){map<int,char> ctr;pair<map<int,int>::const_iterator, map<int,int>::const_iterator> p;int i;for(i=0;i<3;i++){ctr.insert(pair<int,int>(i,i));//给ctr赋值}cout<<"The ctr is: ";print(ctr);//调用子程序来打印ctr的内容cout<<endl;p=ctr.equal_range(2);if(p.first!=ctr.end()){cout<<"The first element which key >= 2 is: ";print_one_item(p.first);//调用子程序来打印一项cout<<endl;}if(p.second!=ctr.end()){cout<<"The first element which key > 2 is: ";print_one_item(p.second);//调用子程序来打印一项cout<<endl;}return 0;}
运行结果如图六所示。
7. map::erase
功能:将一个或一定范围的元素删除。语法:iterator erase(iterator _Where
);
iterator erase(
iterator _First,
iterator _Last
);
size_type erase(
const key_type& _Key
);说明:参见下表函数返回值:前两个函数返回一个指向第一个被删除的元素的双向定位器,如果不存在这样的元素,则返回map容器的末尾。
第三个函数返回被删除的元素的个数。
erase的参数说明。
参数含义_Where表示要删除的元素的位置_First第一个被删除的元素的位置_Last第一个不被删除的元素的位置_Key从map容器中删除的元素的关键字的值因为没有重新分配空间,故只能对应于被删除元素的iterator和reference失效。
注意:他不会抛出任何的exception。
示例。
/***************************************************程序编号: 7程序功能说明:用erase函数将ctr的第二个元素删除。***************************************************/#include <map>#include <iostream>using namespace std;#define LEN 5int print_one_item(map<int,int>::const_iterator cp)//用于打印map的一个元素{cout<<" ("<<cp->first<<", "<<cp->second<<") ";return 0;}int print(map<int,int> c)//用于打印一个map{map<int,int>::const_iterator cp;//让cp从c的开始到结束打印cp对应的值for(cp=c.begin();cp!=c.end();cp++){print_one_item(cp);//调用子程序来打印一个元素return 0;}}int main(){map<int,char> ctr;map<int, int>::iterator cp;int i;for(i=0;i<LEN;i++){ctr.insert(pair<int,int>(i,i));//给ctr赋值}cout<<"The ctr is: ";print(ctr);//调用子程序来打印ctr的内容cout<<endl;count<<"After erase the second element ctr is: ";cp=ctr.begin();cp++;ctr.erase(cp);//擦除ctr的cp位置的元素print(ctr);//调用子程序,把ctr打印出来cout<<endl;return 0;}
运行结果:
运行结果如图7所示。
8. map::find
功能:求出与给定的关键字相等的元素的定位器。语法:iterator find(const Key& _Key
);
const_iterator find(
const Key& _Key
) const;说明:_Key 是要进行搜索的关键字的值。
该函数会返回一个指向关键字为_Key的元素的定位器。
当返回的第一个元素的地址值为一个常值定位器(_iterator),则map可通过它不会被修改。
当返回的第一个元素的地址值为一个定位器(iterator),则map可通过它被修改。
函数返回值:找到该元素时,返回一个指向关键字为_Key的元素的定位器,否则返回一个指向map容器的结束的定位器。
示例:
/***************************************************程序编号: 8程序功能说明:用将Key=2的元素打印出来。***************************************************/#include <map>#include <iostream>using namespace std;#define LEN 5int print_one_item(map<int,int>::const_iterator cp)//用于打印map的一个元素{cout<<" ("<<cp->first<<", "<<cp->second<<") ";return 0;}int print(map<int,int> c)//用于打印一个map{map<int,int>::const_iterator cp;//让cp从c的开始到结束打印cp对应的值for(cp=c.begin();cp!=c.end();cp++){print_one_item(cp);//调用子程序来打印一个元素return 0;}}int main(){map<int,char> ctr;map<int, int>::iterator cp;int i;for(i=0;i<LEN;i++){ctr.insert(pair<int,int>(i,i));//给ctr赋值}cout<<"The ctr is: ";print(ctr);//调用子程序,打印ctr的内容cout<<endl;cp=ctr.find(2);if(cp!=ctr.end()){cout<<"The element whose key = 2 is ";print_one_item(cp);cout<<endl;}else{cout<<"There is no element whose key = 2"<<endl;}return 0;}
运行结果:
运行结果如图8所示。
9. map::get_allocator
功能:返回一个构造该map容器的allocator的一个副本。语法:Allocator get_allocator() const;说明:容器map的allocator指明一个类的存储管理。默认的allocator能提供STL容器高效的运行。函数返回值:改容器的allacator。示例:
/***************************************************程序编号: 9程序功能说明:用ctr的allocator来创建ctr2。***************************************************/#include <map>#include <iostream>using namespace std;int main(){map<int, char> ctr;ctr.insert(pair<int, char>(1, 'a'));map<int, char> ctr2(less<int>(), ctr.get_allocator());cout<<"ctr2's size is: "<<ctr2.size()<<endl;return 0;}
运行结果:
运行结果如图9所示。
10. map::insert
功能:将一个元素或者一定数量的元素插入到map的特定位置中去。语法:pair<iterator, bool> insert(const value_type& _Val
);
iterator insert(
iterator _Where,
const value_type& _Val
);
template<class InputIterator>
void insert(
InputIterator _First,
InputIterator _Last
);说明:下表说明函数返回值:1) 第一个函数返回一对值,当插入成功时,bool=true,
当要插入的元素的关键字与已有的参数的值相同,则bool=false,而iterator 指向插入的位置或者已存在的元素的位置。
2) 第二个函数返回指向插入位置的定位器。
insert的参数说明:
参数含义_Where第一个被插入到map的元素的位置_Val插入的参数的值_First第一个被插入的位置_Last第一个不被插入的位置如果能在_Where后面迅速地插入,那么只要很短的时间。
第三个成员函数将范围为[_First, _Last]中的元素插入。
示例:
/***************************************************程序编号: 10程序功能说明:利用insert函数给ctr赋值。***************************************************/#include <map>#include <iostream>using namespace std;#define LEN 5int print_one_item(map<int,int>::const_iterator cp)//用于打印map的一个元素{cout<<" ("<<cp->first<<", "<<cp->second<<") ";return 0;}int print(map<int,int> c)//用于打印一个map{map<int,int>::const_iterator cp;//让cp从c的开始到结束打印cp对应的值for(cp=c.begin();cp!=c.end();cp++){print_one_item(cp);//调用子程序来打印一个元素return 0;}}int main(){map<int,char> ctr;map<int, int>::iterator cp;int i;for(i=0;i<LEN;i++){ctr.insert(pair<int,int>(i,i));//给ctr赋值}cout<<"The ctr is: ";print(ctr);//调用子程序,打印ctr的内容cout<<endl;return 0;}
运行结果:
运行结果如图10所示。
1 0
- STL map的使用(二)
- STL Map使用详解(二)
- STL map的使用
- map 的使用【STL】
- STL map的使用
- STL map的使用
- STL map的使用
- STL map的使用
- STL map的使用
- stl map的使用
- hdu5199 Gunner(STL map的使用)
- uva156 Ananagrams (stl map的使用)
- STL map的使用(一)
- STL中map的使用
- C++ STL map的使用
- C++ STL map的使用
- STL中 MAP的使用
- STL map的使用 POJ2503
- Android开发中将LOG存入SD卡
- C++primer阅读笔记-模板与泛型编程(重载与模板)
- Delphi 事件 --事件原来是函数指针,同时是个属性
- adjacent_find
- iOS KVC & KVO
- STL map的使用(二)
- SecureZeroMemory和ZeroMemory的区别
- ToolBar居中显示文字或控件
- CSS margin 属性
- 入手Cubieboard2之制作最小Linux系统
- Hibernate – One-to-One example (Annotation)
- hadoop学习之hadoop完全分布式集群安装
- nginx启用php
- ashx入门