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;
}



 

相关文章
|
存储 Linux C语言
Linux C/C++之IO多路复用(aio)
这篇文章介绍了Linux中IO多路复用技术epoll和异步IO技术aio的区别、执行过程、编程模型以及具体的编程实现方式。
907 1
Linux C/C++之IO多路复用(aio)
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
565 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`),全面解析它们的特点、用法
1015 1
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
消息中间件 Linux C++
c++ linux通过实现独立进程之间的通信和传递字符串 demo
的进程间通信机制,适用于父子进程之间的数据传输。希望本文能帮助您更好地理解和应用Linux管道,提升开发效率。 在实际开发中,除了管道,还可以根据具体需求选择消息队列、共享内存、套接字等其他进程间通信方
444 16
|
编译器 程序员 定位技术
C++ 20新特性之Concepts
在C++ 20之前,我们在编写泛型代码时,模板参数的约束往往通过复杂的SFINAE(Substitution Failure Is Not An Error)策略或繁琐的Traits类来实现。这不仅难以阅读,也非常容易出错,导致很多程序员在提及泛型编程时,总是心有余悸、脊背发凉。 在没有引入Concepts之前,我们只能依靠经验和技巧来解读编译器给出的错误信息,很容易陷入“类型迷路”。这就好比在没有GPS导航的年代,我们依靠复杂的地图和模糊的方向指示去一个陌生的地点,很容易迷路。而Concepts的引入,就像是给C++的模板系统安装了一个GPS导航仪
515 59
|
安全 编译器 C++
【C++11】新特性
`C++11`是2011年发布的`C++`重要版本,引入了约140个新特性和600个缺陷修复。其中,列表初始化(List Initialization)提供了一种更统一、更灵活和更安全的初始化方式,支持内置类型和满足特定条件的自定义类型。此外,`C++11`还引入了`auto`关键字用于自动类型推导,简化了复杂类型的声明,提高了代码的可读性和可维护性。`decltype`则用于根据表达式推导类型,增强了编译时类型检查的能力,特别适用于模板和泛型编程。
290 2
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
3266 3
|
资源调度 Linux 调度
Linux C/C++之线程基础
这篇文章详细介绍了Linux下C/C++线程的基本概念、创建和管理线程的方法,以及线程同步的各种机制,并通过实例代码展示了线程同步技术的应用。
417 0
Linux C/C++之线程基础
|
Linux C++
Linux C/C++之IO多路复用(poll,epoll)
这篇文章详细介绍了Linux下C/C++编程中IO多路复用的两种机制:poll和epoll,包括它们的比较、编程模型、函数原型以及如何使用这些机制实现服务器端和客户端之间的多个连接。
811 0
Linux C/C++之IO多路复用(poll,epoll)
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
293 0