您的位置:首页 > 编程语言 > C语言/C++

C++之STL(6)之 map 与 multimap 关联容器

2016-07-27 00:03 513 查看
#include<iostream>
#include<map>
#include<string>

using namespace std;

int main()
{
map<int, string> a;
multimap<int, string> ma;

// 插入 3种方法                       key键   value 值
a.insert(map<int, string>::value_type(1, "One"));
a.insert(map<int, string>::value_type(2, "Two"));
a.insert(map<int, string>::value_type(3, "Three"));
a.insert(make_pair(-1, "Minus One"));
a.insert(pair<int, string>(1000, "One Thousand"));

a[1000000] = "One Million";

cout << "map里一共有" << a.size() << "个key-value对数据";
cout << "这些数据是:" << endl;
map<int, string>::const_iterator i;
for (i = a.begin(); i != a.end(); ++i)
{
cout << "Key:" << i->first;
cout << "Value:" << i->second.c_str();
cout << endl;
}

ma.insert(multimap<int, string>::value_type(3, "Three"));
ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
ma.insert(make_pair(-1, "Minus One"));
ma.insert(pair<int, string>(1000, "One Thousand"));
ma.insert(pair<int, string>(1000, "One Thousand"));

cout << endl << "multimap里面有" << ma.size() << "个数" << endl;

multimap<int, string>::const_iterator im;
for (im = ma.begin(); im != ma.end(); im++)
{
cout << "Key:" << im->first;
cout << "Value:" << im->second.c_str();
cout << endl;
}

cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl;

system("pause");
return 0;
}

总结:

3种插入方法:

a.insert(map<int, string>::value_type(1, "One"));
a.insert(make_pair(-1, "Minus One"));
a.insert(pair<int, string>(1000, "One Thousand"));
count方法

cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl;
#include<iostream>
#include<map>
#include<string>

using namespace std;

int main()
{
map<int, string> a;
map<string, int> m_Score;
multimap<int, string> ma;

// 插入 3种方法                       key键   value 值
a.insert(map<int, string>::value_type(1, "One"));
a.insert(map<int, string>::value_type(2, "Two"));
a.insert(map<int, string>::value_type(3, "Three"));
a.insert(make_pair(-1, "Minus One"));
a.insert(pair<int, string>(1000, "One Thousand"));

a[1000000] = "One Million";

m_Score.insert(make_pair("张飞", 99));
m_Score.insert(make_pair("刘备", 56));
m_Score["关羽"] = 87;

// 对map进行查找
cout << "最简单的查找:" << endl;
cout << m_Score["刘备"] << endl;
cout << a[3] << endl;

cout << "map里一共有" << a.size() << "个key-value对数据";
cout << "这些数据是:" << endl;
map<int, string>::const_iterator i;
for (i = a.begin(); i != a.end(); ++i)
{
cout << "Key:" << i->first;
cout << "Value:" << i->second.c_str();
cout << endl;
}

ma.insert(multimap<int, string>::value_type(3, "Three"));
ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
ma.insert(make_pair(-1, "Minus One"));
ma.insert(pair<int, string>(1000, "One Thousand"));
ma.insert(pair<int, string>(1000, "One Thousand"));

cout << endl << "multimap里面有" << ma.size() << "个数" << endl;

multimap<int, string>::const_iterator im;
for (im = ma.begin(); im != ma.end(); im++)
{
cout << "Key:" << im->first;
cout << "Value:" << im->second.c_str();
cout << endl;
}

cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl;

// 查找
multimap<int, string>::iterator fi;
// 不能通过迭代器进行修改
fi =  ma.find(45);
if (fi != ma.end())
{
cout << "找到了!" << fi->first << "=" << fi->second.c_str() << endl;;
}
else
cout << "没有找到!" << endl;

// 找重复的
fi = ma.find(1000);
if (fi != ma.end())
{
cout << "找到了1000!" << endl;
size_t n = ma.count(1000);
for (size_t i = 0; i < n; i++)
{
cout << "\t Key:" << fi->first;
cout << ", Value [" << i << "] = ";
cout << fi->second.c_str() << endl;
++fi;
}
}
else
{
cout << "没找到1000!" << endl;
}

system("pause");
return 0;
}




#include<iostream>
#include<map>
#include<string>

using namespace std;

int main()
{
map<int, string> a;
map<string, int> m_Score;
multimap<int, string> ma;

// 插入 3种方法                       key键   value 值
a.insert(map<int, string>::value_type(1, "One"));
a.insert(map<int, string>::value_type(2, "Two"));
a.insert(map<int, string>::value_type(3, "Three"));
a.insert(make_pair(-1, "Minus One"));
a.insert(pair<int, string>(1000, "One Thousand"));

a[1000000] = "One Million";

m_Score.insert(make_pair("张飞", 99));
m_Score.insert(make_pair("刘备", 56));
m_Score["关羽"] = 87;// 这个方法是用的最多的

// 对map进行查找
cout << "最简单的查找:" << endl;
cout << m_Score["刘备"] << endl;
cout << a[3] << endl;

cout << "map里一共有" << a.size() << "个key-value对数据";
cout << "这些数据是:" << endl;
map<int, string>::const_iterator i;
for (i = a.begin(); i != a.end(); ++i)
{
cout << "Key:" << i->first;
cout << "Value:" << i->second.c_str();
cout << endl;
}

ma.insert(multimap<int, string>::value_type(3, "Three"));
ma.insert(multimap<int, string>::value_type(45, "Forty Five"));
ma.insert(make_pair(-1, "Minus One"));
ma.insert(pair<int, string>(1000, "One Thousand"));
ma.insert(pair<int, string>(1000, "One Thousand"));

cout << endl << "multimap里面有" << ma.size() << "个数" << endl;

multimap<int, string>::const_iterator im;
for (im = ma.begin(); im != ma.end(); im++)
{
cout << "Key:" << im->first;
cout << "Value:" << im->second.c_str();
cout << endl;
}

cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl;

// 查找
multimap<int, string>::iterator fi;
// 不能通过迭代器进行修改
fi =  ma.find(45);
if (fi != ma.end())
{
cout << "找到了!" << fi->first << "=" << fi->second.c_str() << endl;;
}
else
cout << "没有找到!" << endl;

// 找重复的
fi = ma.find(1000);
if (fi != ma.end())
{
cout << "找到了1000!" << endl;
size_t n = ma.count(1000);
for (size_t i = 0; i < n; i++)
{
cout << "\t Key:" << fi->first;
cout << ", Value [" << i << "] = ";
cout << fi->second.c_str() << endl;
++fi;
}
}
else
{
cout << "没找到1000!" << endl;
}

// 删除方法
if (ma.erase(-1) > 0)
{
cout << "删除成功-1!" << endl;
}
// 删除方法
multimap<int, string>::iterator iEllementFound = ma.find(45);
if (iEllementFound != ma.end())
{
ma.erase(iEllementFound);
cout << "删除45成功" << endl;
}
// 删除方法
ma.erase(ma.lower_bound(1000), ma.upper_bound(1000));// 删除一个范围

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: