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

简介: /*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;
}



 

相关文章
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
773 12
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
1230 1
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
编译器 程序员 定位技术
C++ 20新特性之Concepts
在C++ 20之前,我们在编写泛型代码时,模板参数的约束往往通过复杂的SFINAE(Substitution Failure Is Not An Error)策略或繁琐的Traits类来实现。这不仅难以阅读,也非常容易出错,导致很多程序员在提及泛型编程时,总是心有余悸、脊背发凉。 在没有引入Concepts之前,我们只能依靠经验和技巧来解读编译器给出的错误信息,很容易陷入“类型迷路”。这就好比在没有GPS导航的年代,我们依靠复杂的地图和模糊的方向指示去一个陌生的地点,很容易迷路。而Concepts的引入,就像是给C++的模板系统安装了一个GPS导航仪
687 59
|
存储 搜索推荐 C++
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
509 9
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器2
|
安全 编译器 C++
【C++11】新特性
`C++11`是2011年发布的`C++`重要版本,引入了约140个新特性和600个缺陷修复。其中,列表初始化(List Initialization)提供了一种更统一、更灵活和更安全的初始化方式,支持内置类型和满足特定条件的自定义类型。此外,`C++11`还引入了`auto`关键字用于自动类型推导,简化了复杂类型的声明,提高了代码的可读性和可维护性。`decltype`则用于根据表达式推导类型,增强了编译时类型检查的能力,特别适用于模板和泛型编程。
424 2
|
存储 C++ 容器
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器1
【C++篇】深度剖析C++ STL:玩转 list 容器,解锁高效编程的秘密武器
490 5
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
460 2
|
存储 编译器 C++
【C++】面向对象编程的三大特性:深入解析多态机制(三)
【C++】面向对象编程的三大特性:深入解析多态机制
303 1
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
365 0
C++ 20新特性之结构化绑定
在C++ 20出现之前,当我们需要访问一个结构体或类的多个成员时,通常使用.或->操作符。对于复杂的数据结构,这种访问方式往往会显得冗长,也难以理解。C++ 20中引入的结构化绑定允许我们直接从一个聚合类型(比如:tuple、struct、class等)中提取出多个成员,并为它们分别命名。这一特性大大简化了对复杂数据结构的访问方式,使代码更加清晰、易读。
473 0