关于容器map的操作代码
来源:互联网 发布:树莓派3 ubuntu 编辑:程序博客网 时间:2024/06/05 11:15
#include<iostream>
#include<map>
#include<iterator>
using namespace std;
int main()
{
typedef map<int,string> M;
M my_map;
/*-----say how to insert value in the map*/
//Example for some way to insert value in the map
//1.the first is the subscript operation
my_map[1]="frist";
//2.the second way
my_map.insert(M::value_type(2,"second"));
//3.
my_map.insert(pair<int,string>(3,"three"));
//4.
my_map.insert(make_pair(4,"four"));
/*-----show the map-----*/
M::iterator It=my_map.begin(); //define a iterato
//printf the value of map
cout<<"the value of map"<<endl;
while(It!=my_map.end())
{
cout<<It->first<<"--"<<It->second<<endl;
It++;
}
cout<<endl;
/*-----search a value in the map----*/
//1.subscript operation
string str=my_map[2]; //search a value of the key is 2
my_map[2]="second_change"; //change the value of the key is 2
cout<<"first search way:the change value map[2]: "
<<my_map[2]<<endl;
//2.through the iterator
M::iterator search_map=my_map.find(3);
str=search_map->second;
search_map->second="find you";
cout<<"second search way:the change value :"<<search_map->second<<endl;
//!!!!! BUT be careful the subscript operation
//if the key not in the map,it can insert a new key in the map
//the value is default value
str=my_map[5];
It=my_map.begin();
while(It!=my_map.end())
{
cout<<It->first<<"__"<<It->second<<endl;
It++;
}
cout<<endl;
/*-----delete the value----*/
//delete of the iterator
my_map.erase(search_map);
//delete of the value
my_map.erase(4);
It=my_map.begin();
while(It!=my_map.end())
{
cout<<It->first<<"--"<<It->second<<endl;
It++;
}
cout<<endl;
/*-----other way in map-----*/
//return count
cout<<"my_map size is:"<<my_map.size()<<endl;
//clear the map
my_map.clear();
//contrast the map
if(my_map.empty())
cout<<"the map is clear,now is empty"<<endl;
getchar();
return 0;
#include<map>
#include<iterator>
using namespace std;
int main()
{
typedef map<int,string> M;
M my_map;
/*-----say how to insert value in the map*/
//Example for some way to insert value in the map
//1.the first is the subscript operation
my_map[1]="frist";
//2.the second way
my_map.insert(M::value_type(2,"second"));
//3.
my_map.insert(pair<int,string>(3,"three"));
//4.
my_map.insert(make_pair(4,"four"));
/*-----show the map-----*/
M::iterator It=my_map.begin(); //define a iterato
//printf the value of map
cout<<"the value of map"<<endl;
while(It!=my_map.end())
{
cout<<It->first<<"--"<<It->second<<endl;
It++;
}
cout<<endl;
/*-----search a value in the map----*/
//1.subscript operation
string str=my_map[2]; //search a value of the key is 2
my_map[2]="second_change"; //change the value of the key is 2
cout<<"first search way:the change value map[2]: "
<<my_map[2]<<endl;
//2.through the iterator
M::iterator search_map=my_map.find(3);
str=search_map->second;
search_map->second="find you";
cout<<"second search way:the change value :"<<search_map->second<<endl;
//!!!!! BUT be careful the subscript operation
//if the key not in the map,it can insert a new key in the map
//the value is default value
str=my_map[5];
It=my_map.begin();
while(It!=my_map.end())
{
cout<<It->first<<"__"<<It->second<<endl;
It++;
}
cout<<endl;
/*-----delete the value----*/
//delete of the iterator
my_map.erase(search_map);
//delete of the value
my_map.erase(4);
It=my_map.begin();
while(It!=my_map.end())
{
cout<<It->first<<"--"<<It->second<<endl;
It++;
}
cout<<endl;
/*-----other way in map-----*/
//return count
cout<<"my_map size is:"<<my_map.size()<<endl;
//clear the map
my_map.clear();
//contrast the map
if(my_map.empty())
cout<<"the map is clear,now is empty"<<endl;
getchar();
return 0;
}
result:
the value of map
1--frist
2--second
3--three
4--four
first search way:the change value map[2]: second_change
second search way:the change value :find you
1__frist
2__second_change
3__find you
4__four
5__
1--frist
2--second_change
5--
my_map size is:3
the map is clear,now is empty
- 关于容器map的操作代码
- map容器的基本操作
- map容器的简单操作
- STL中map容器的操作
- STL容器map的一段代码
- 关于std::Map容器的一点总结
- 关于容器Map删除元素的常见问题
- 关于容器的各种操作
- 关于map容器
- set容器与map容器基本操作
- 关于map容器的insert成员的返回值说明
- C++容器map的基本操作和使用
- 关于c++ list容器的操作摸索
- 关于C++ 容器的swap操作
- map容器的基本用法(代码版)
- 关于C++ map容器迭代器越界的研究
- C++关于容器Map删除元素的常见问题
- 关于Map的操作(转载)
- (2)提示错误:当前记录集不支持更新。这可能是提供程序的限制,也可能是选定锁定类型的限制。
- 用仿ActionScript的语法来编写html5——第六篇,TextField与输入框
- wamp修改mysql数据库root密码
- Windows窗体控件线程安全
- DataRelation(DataSet中父子表)的一个示例
- 关于容器map的操作代码
- 在java + oracle环境下,对于clob类型的数据的插入
- 触发器
- launcher修改---launcher的搜索框和ProtipWidget(widget修改)(有图有真相)
- 平衡二叉搜索树(AVL树)
- 浅谈C中的malloc和free
- 实习日记041(9月16日)
- VSS+SAW配置
- 数据库编程实战:Oracle PL/SQL入门之案例实践