linux下练习 c++ 关联式容器map特性

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: /*map.cppmap特性不允许key重复key/value对key可以当下标访问value,key不存在则插入新的key/value对,以0初始化*/#include#include#include "print.
/*
map.cpp
map特性
不允许key重复
key/value对
key可以当下标访问value,key不存在则插入新的key/value对,以0初始化
*/
#include<iostream>
#include<string>
#include "print.h"
#include<map>
using namespace std;
typedef pair<int,string>  pairmp;
#include<map>
int main()
{
	map<int,string> mp;
	mp.insert(pair<int,string>(1,"aaa"));
	mp.insert(make_pair(5,"bbb"));//自动匹配类型,构造pair
	mp.insert(map<int,string>::value_type(4,"fff"));//内部类型,也能自动构造相应的pair
	mp.insert(make_pair(2,"hhh"));
	mp.insert(make_pair(2,"hhh"));
	mp[2]="hhh1";//有则修改
	mp[3]="ddd";//无则插入
	print(mp.begin(),mp.end());
	return 0;
}


print.h

//print.h

#include <iostream>

using namespace std;

#ifndef print_fun

#define print_fun

template<typename T>

///显示序列数据

void print(T b,T e,char c=' ')

{

	bool isExit=false;

	while (b!=e)

	{

		cout<<*b++<<c;

		isExit=true;

	}

	if(isExit) cout<<endl;



}
template<typename K,typename V>
ostream& operator<<(ostream& o,const pair<K,V>& p)//重载输出map类型元素
{
	return o<<p.first<<':'<<p.second;
}

#endif


 

 

#include<iostream>
using namespace std;
#include<fstream>
#include<map>
#include<cstring>
class Candidate{
public:
	Candidate(const string& name=""):m_name(name),m_votes(0){}
	Candidate(const char* name):m_name(name),m_votes(0){}
	const string& name() const{
		return m_name;
	}
	const int& votes() const{
		return m_votes;
	}
	void vote(){
		++ m_votes;
	}
private:
	string m_name;
	int m_votes;
};
//测试投票
void test1(){
	map<char,Candidate> mcc;
	mcc.insert(make_pair('a',Candidate("zhangsan")));
	mcc.insert(pair<char,Candidate>('f',"zhaoyun"));
	mcc['b']=Candidate("lisi");
	typedef map<char,Candidate>::iterator IT;
	typedef map<char,Candidate>::const_iterator CIT;
	for(int i=0;i<5;i++)
	{
		for(CIT it=mcc.begin();it!=mcc.end();it++)
		{
			//it->second.vote();
			cout<<it->first<<":"<<it->second.name()<<"  ";
		}
		cout<<endl<<"请投票:";
		char key;
		cin>>key;
		IT fit=mcc.find(key);

		if(fit==mcc.end()) continue;
		
		fit->second.vote();
		cout<<"投了一票给"<<fit->first<<endl;
		
	}
	CIT win=mcc.begin();
	for(CIT it=mcc.begin();it!=mcc.end();it++){
		cout<<it->second.name()<<":"
				<<it->second.votes()<<endl;
		if(it->second.votes()>win->second.votes()) win=it;
	}
	cout<<"最多票数:"<<win->second.name()<<endl;

}
class StrCmp{//让字符串大小写不区分
	public:
		bool operator()(const string& a,const string& b){
			return strcasecmp(a.c_str(),b.c_str())<0;
		}
		bool operator()(const char* a,const char* b){
			return strcasecmp(a,b)<0;
		}
};
//统计每个单词出现的次数
void test2()
{
	ifstream ifs("words.txt");
	map<string,int,StrCmp> msi;
	string word;
	while(ifs>>word) msi[word]++;
	ifs.close();
	typedef map<string,int>::iterator IT;
	cout<<"一共"<<msi.size()<<"个单词"<<endl;
	for(IT it=msi.begin();it!=msi.end();it++)
	{
		cout<<it->first<<":"<<it->second<<"\n";
	}
}
int main()
{
	//test1();
	test2();
	return 0;
}



 

相关文章
|
2天前
|
C语言 C++ 开发者
深入探索C++:特性、代码实践及流程图解析
深入探索C++:特性、代码实践及流程图解析
|
2天前
|
存储 编译器 C++
C++:map&set 对红黑树的封装
C++:map&set 对红黑树的封装
10 1
|
2天前
|
存储 C++ 容器
C++:STL - set & map
C++:STL - set & map
15 4
|
2天前
|
自然语言处理 编译器 C语言
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
本文章是我对C++学习的开始,很荣幸与大家一同进步。 首先我先介绍一下C++,C++是上个世纪为了解决软件危机所创立 的一项面向对象的编程语言(OOP思想)。
35 1
【C++】C++ 入门 — 命名空间,输入输出,函数新特性
|
2天前
|
JSON Java Linux
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
21 2
|
2天前
|
存储 安全 算法
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】基于环形队列的多生产者多消费者模型(Linux系统下C++ 代码模拟实现)
26 0
|
2天前
|
算法 Linux 数据安全/隐私保护
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
【Linux | C++ 】生产者消费者模型(Linux系统下C++ 代码模拟实现)
13 0
|
2天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
14 1
|
2天前
|
存储 Linux 文件存储
Linux使用Docker部署Traefik容器并实现远程访问管理界面-1
Linux使用Docker部署Traefik容器并实现远程访问管理界面
|
2天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。