首页 > 其他分享 >数据结构(1) pair和map使用

数据结构(1) pair和map使用

时间:2022-11-29 11:14:43浏览次数:43  
标签:map include ma insert pair 数据结构 my

 

 

 

 

 

#include <iostream>

#include <thread>

#include<map>

#include <algorithm>

#include <vector>

#ifdef lniux 
#include <unistd.h> //usleep(1000000);
#else
#include <Windows.h>
#include <WinBase.h>
#endif



using namespace std;




void test1_pair() {
	// pair测试
	typedef pair<string, string> Author;
	Author proust("March", "Proust");
	cout << "pair 测试  first:" << proust.first << "   second:  " << proust.second << endl;

}

void test1_insert() {

	//第一种:用insert函数插入pair数据:
	map<int, string> my_map;
	my_map.insert(pair<int, string>(1, "first"));
	my_map.insert(pair<int, string>(2, "second"));

	my_map.insert(map<int, string>::value_type(3, "2"));
	my_map.insert(map<int, string>::value_type(4, "3"));

	my_map[5] = "5";
	my_map[6] = "6";

	//std::cout << my_map[1] << endl;
	//std::cout << my_map[2] << endl;

	map<int, string>::iterator it;
	for (it = my_map.begin(); it != my_map.end(); it++)
		cout << "map测试 first:  " << it->first << " second: " << it->second << endl;




}

void test2_find() {

	//查找 判断关键字是否出现

	map< string, int> myfing_map;
	myfing_map.insert(pair<string, int>("dongdong", 21));
	myfing_map.insert(pair<string, int>("xixi", 22));
	myfing_map.insert(pair<string, int>("lala", 23));
	map<string, int>::iterator it1;
	it1 = myfing_map.find("dongdong");
	if (it1 != myfing_map.end())
		cout << "Find, the value is " << it1->second << endl;
	else
		cout << "Do not Find" << endl;

}

void test4_erase()
{
	map<int, string> my_map;
	my_map.insert(pair<int, string>(1, "one"));
	my_map.insert(pair<int, string>(2, "two"));
	my_map.insert(pair<int, string>(3, "three"));
	//如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好
	//如果要删除1,用迭代器删除
	map<int, string>::iterator it;
	it = my_map.find(1);
	my_map.erase(it);                   //如果要删除1,用关键字删除
	int n = my_map.erase(1);            //如果删除了会返回1,否则返回0
	//用迭代器,成片的删除
	//一下代码把整个map清空
	my_map.erase(my_map.begin(), my_map.end());
	//成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
	//自个加上遍历代码,打印输出吧
	
}




bool cmp(pair<string, int> a, pair<string, int> b) {
	return a.second < b.second;
}

void test3_sort() {

	// 排序
	map<string, int> ma;
	ma["Alice"] = 86;
	ma["Bob"] = 78;
	ma["Zip"] = 92;
	ma["Stdevn"] = 88;
	//vector< pair<string, int> > vec(ma.begin(), ma.end());

	vector< pair<string, int> > vec;
	for (map<string, int>::iterator it = ma.begin(); it != ma.end(); it++)
		vec.push_back(pair<string, int>(it->first, it->second));

	sort(vec.begin(), vec.end(), cmp);
	for (vector< pair<string, int> >::iterator it = vec.begin(); it != vec.end(); ++it)
	{
		cout << it->first << " " << it->second << endl;

	}


}



int main(int argc, char** argv)
{

	test1_pair();
	test1_insert();
	test2_find();
	test3_sort();
	test4_erase();

}

  

标签:map,include,ma,insert,pair,数据结构,my
From: https://www.cnblogs.com/gooutlook/p/16934830.html

相关文章

  • 【剑指Offer】数据结构
    文章目录​​励志​​​​一、剑指Offer05.替换空格​​​​题:​​​​解:​​​​二、剑指Offer06.从尾到头打印链表​​​​题:​​​​解:​​​​三、剑指Offer09.......
  • OCC BRepPrimAPI_MakeBox实现步骤记录
    OCC构建Box实现过程解析:TopoDS_Shape=BRepPrimAPI_MakeBox(5,5,5); //BRepPrimAPI_MakeBox简单介绍BRepPrimAPI_MakeBox:publicBRepBuilderAPI_MakeShape{p......
  • 用 LSN 画个 PAGE MAP
    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。GreatSQL是MySQL的国产分支版本,使用上与MySQL一致。作者:花家舍想法从何而来获得雨果奖的科幻......
  • PTA 21级数据结构与算法实验8—排序
    目录7-1统计工龄7-2寻找大富翁7-3点赞狂魔7-4插入排序还是归并排序7-5插入排序还是堆排序7-6逆序对7-7堆排序7-8石子合并7-9第k小7-10快速排序的过程7-1统计工......
  • go源码学习(一):数据结构-数组
    数组是相同类型元素的集合,在内存中对应一块连续的内存空间。数组类型是通过存储的元素类型以及能够存储的大小两个维度来决定的,一旦声明之后大小就不可更改。初始化go语......
  • 【779】R语言数据结构
    1.向量向量从数据结构上看就是一个线性表,可以看成一个数组。c()是一个创造向量的函数。R语言中的"下标"不代表偏移量,而代表第几个,也就是说是从1开始的!seq......
  • HashMap实现原理和自动扩容
    HashMap实现原理:JDK1.7:数组+单向链表(头插)在并发情况下头插可能出现循环链表(死循环)问题。原因:因为头插,在新数组中链表的元素顺序发生了变化,  如上图,假设线程1在扩......
  • org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 解决方法搜
    错误代码:org.hibernate.MappingException:NoDialectmappingforJDBCtype:-1atorg.hibernate.dialect.TypeNames.get(TypeNames.java:79)atorg.hibernate.di......
  • Python|批量获取Mapbox等时圈shapefile格式数据
    直接获取Mapbox出行圈数据Mapbox地图网站有个IsochoneAPI接口IsochroneAPI|Playground|Mapbox,可以快速获得等时圈数据。获取accesstoken打开Mapbox网站Maps,geoc......
  • Python|批量获取mapbox等时圈shapefile格式数据
    直接获取mapbox出行圈数据mapbox地图网站有个IsochoneAPI接口IsochroneAPI|Playground|Mapbox,可以快速获得等时圈数据。获取accesstoken打开mapbox网站Maps,geoc......