/*
map相对于set的区别,map具有键值和实值,所有元素根据键值自动排序,pair的第一元素被称为键值,第二元素被称为实值,map也是以红黑树为底层实现机制
//map构造函数
map<T1, T2> mapTT;//map默认构造函数
map(const map &mp);//拷贝构造函数
//map赋值操作
map赋值操作map& operator=(const map &mp)//重载等号操作符
swap(mp);//交换俩个集合容器
map大小操作
size();//返回容器中元素的数目
cmpty();//判断容器是否为空
map插入数据元素操作
map.insert(...);//往容器插入元素,返回pair<iterator, bool>
map<int, string>mapStu
*/
# include <iostream>
# include <map>
using namespace std;
//初始化
void test01()
{
//map容器模板参数,第一个参数key的类型,第二个参数value类型
map<int, int> mymap;
//插入数据 pair.first key值, pair.second value 值
//第一种
mymap.insert(pair<int , int>(10, 10));
pair<map<int, int>:: iterator, bool> ret = mymap.insert(pair<int, int>(10, 10));
if(ret.second)
{
cout << "第一次插入成功!" << endl;
cout << ret.second << endl;
}
else
{
cout << "插入失败" << endl;
}
ret = mymap.insert(pair<int, int>(10, 20));
if(ret.second)
{
cout << "第一次插入成功!" << endl;
cout << ret.second << endl;
}
else
{
cout << "插入失败" << endl;
}
//第二种
mymap.insert(make_pair(20, 20));
//第三种
mymap.insert(map<int, int>::value_type(30, 30));
//第四种
mymap[40] = 40;
mymap[10] = 20;
mymap[50] = 50;
//如果key不存在,创建pair插入到map容器中
//如果发现key存在,那么会修改key对应的value
//打印
for(map<int, int>:: iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it 取出来的是一个pair
cout << "key:" <<it->first << endl;
cout << " value:" <<it->second << endl;
}
//如果通过[]方式去访问map中一个不存在的key
//那么map会将这个访问的key插入到map中,并且给value一个值
cout << "mymap[60]:" << mymap[60] << endl;
for(map<int, int>:: iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it 取出来的是一个pair
cout << "key:" <<it->first << endl;
cout << " value:" <<it->second << endl;
}
}
class MyKey
{
public:
MyKey(int index, int id)
{
this->mID = id;
this->mIndex = index;
}
public:
int mIndex;
int mID;
};
struct mycompare
{
bool operator()(MyKey key1, MyKey key2)
{
return key1.mIndex > key2.mIndex;
}
};
void test02()
{
map<MyKey, int, mycompare> mymap;//自动排序 自定义数据类型咋排
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(4, 5), 20));
for(map<MyKey, int, mycompare>:: iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it 取出来的是一个pair
cout << it->first.mID << ":" << it->first.mIndex << "=" << it->second << endl;
}
}
//equal_range
void test03()
{
map<int, int> mymap;
mymap.insert(make_pair(1, 4));
mymap.insert(make_pair(2, 5));
mymap.insert(make_pair(3, 6));
pair<map<int, int> :: iterator, map<int, int> :: iterator> ret = mymap.equal_range(2);
if(ret.first->second)
{
cout << "找到了!" << endl;
cout << "ret.first->second:" << ret.first->second << endl;
}
else
{
cout << "没有找到" << endl;
}
if(ret.second->second)
{
cout << "找到了upper_bound!" << endl;
cout << "ret.second->second:" << ret.first->second << endl;
}
else
{
cout << "没有找到" << endl;
}
}
int main(int argc, char *argv[])
{
test01();
cout << "------------" << endl;
test02();
cout << "------------" << endl;
test03();
return 0;
}
map相对于set的区别,map具有键值和实值,所有元素根据键值自动排序,pair的第一元素被称为键值,第二元素被称为实值,map也是以红黑树为底层实现机制
//map构造函数
map<T1, T2> mapTT;//map默认构造函数
map(const map &mp);//拷贝构造函数
//map赋值操作
map赋值操作map& operator=(const map &mp)//重载等号操作符
swap(mp);//交换俩个集合容器
map大小操作
size();//返回容器中元素的数目
cmpty();//判断容器是否为空
map插入数据元素操作
map.insert(...);//往容器插入元素,返回pair<iterator, bool>
map<int, string>mapStu
*/
# include <iostream>
# include <map>
using namespace std;
//初始化
void test01()
{
//map容器模板参数,第一个参数key的类型,第二个参数value类型
map<int, int> mymap;
//插入数据 pair.first key值, pair.second value 值
//第一种
mymap.insert(pair<int , int>(10, 10));
pair<map<int, int>:: iterator, bool> ret = mymap.insert(pair<int, int>(10, 10));
if(ret.second)
{
cout << "第一次插入成功!" << endl;
cout << ret.second << endl;
}
else
{
cout << "插入失败" << endl;
}
ret = mymap.insert(pair<int, int>(10, 20));
if(ret.second)
{
cout << "第一次插入成功!" << endl;
cout << ret.second << endl;
}
else
{
cout << "插入失败" << endl;
}
//第二种
mymap.insert(make_pair(20, 20));
//第三种
mymap.insert(map<int, int>::value_type(30, 30));
//第四种
mymap[40] = 40;
mymap[10] = 20;
mymap[50] = 50;
//如果key不存在,创建pair插入到map容器中
//如果发现key存在,那么会修改key对应的value
//打印
for(map<int, int>:: iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it 取出来的是一个pair
cout << "key:" <<it->first << endl;
cout << " value:" <<it->second << endl;
}
//如果通过[]方式去访问map中一个不存在的key
//那么map会将这个访问的key插入到map中,并且给value一个值
cout << "mymap[60]:" << mymap[60] << endl;
for(map<int, int>:: iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it 取出来的是一个pair
cout << "key:" <<it->first << endl;
cout << " value:" <<it->second << endl;
}
}
class MyKey
{
public:
MyKey(int index, int id)
{
this->mID = id;
this->mIndex = index;
}
public:
int mIndex;
int mID;
};
struct mycompare
{
bool operator()(MyKey key1, MyKey key2)
{
return key1.mIndex > key2.mIndex;
}
};
void test02()
{
map<MyKey, int, mycompare> mymap;//自动排序 自定义数据类型咋排
mymap.insert(make_pair(MyKey(1, 2), 10));
mymap.insert(make_pair(MyKey(4, 5), 20));
for(map<MyKey, int, mycompare>:: iterator it = mymap.begin(); it != mymap.end(); it++)
{
//*it 取出来的是一个pair
cout << it->first.mID << ":" << it->first.mIndex << "=" << it->second << endl;
}
}
//equal_range
void test03()
{
map<int, int> mymap;
mymap.insert(make_pair(1, 4));
mymap.insert(make_pair(2, 5));
mymap.insert(make_pair(3, 6));
pair<map<int, int> :: iterator, map<int, int> :: iterator> ret = mymap.equal_range(2);
if(ret.first->second)
{
cout << "找到了!" << endl;
cout << "ret.first->second:" << ret.first->second << endl;
}
else
{
cout << "没有找到" << endl;
}
if(ret.second->second)
{
cout << "找到了upper_bound!" << endl;
cout << "ret.second->second:" << ret.first->second << endl;
}
else
{
cout << "没有找到" << endl;
}
}
int main(int argc, char *argv[])
{
test01();
cout << "------------" << endl;
test02();
cout << "------------" << endl;
test03();
return 0;
}
1531

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



