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
Linux C/C++之IO多路复用(select)
这篇文章主要介绍了TCP的三次握手和四次挥手过程,TCP与UDP的区别,以及如何使用select函数实现IO多路复用,包括服务器监听多个客户端连接和简单聊天室场景的应用示例。
604 0
|
存储 Linux C语言
Linux C/C++之IO多路复用(aio)
这篇文章介绍了Linux中IO多路复用技术epoll和异步IO技术aio的区别、执行过程、编程模型以及具体的编程实现方式。
1031 1
Linux C/C++之IO多路复用(aio)
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
701 12
|
编译器 程序员 定位技术
C++ 20新特性之Concepts
在C++ 20之前,我们在编写泛型代码时,模板参数的约束往往通过复杂的SFINAE(Substitution Failure Is Not An Error)策略或繁琐的Traits类来实现。这不仅难以阅读,也非常容易出错,导致很多程序员在提及泛型编程时,总是心有余悸、脊背发凉。 在没有引入Concepts之前,我们只能依靠经验和技巧来解读编译器给出的错误信息,很容易陷入“类型迷路”。这就好比在没有GPS导航的年代,我们依靠复杂的地图和模糊的方向指示去一个陌生的地点,很容易迷路。而Concepts的引入,就像是给C++的模板系统安装了一个GPS导航仪
609 59
|
安全 编译器 C++
【C++11】新特性
`C++11`是2011年发布的`C++`重要版本,引入了约140个新特性和600个缺陷修复。其中,列表初始化(List Initialization)提供了一种更统一、更灵活和更安全的初始化方式,支持内置类型和满足特定条件的自定义类型。此外,`C++11`还引入了`auto`关键字用于自动类型推导,简化了复杂类型的声明,提高了代码的可读性和可维护性。`decltype`则用于根据表达式推导类型,增强了编译时类型检查的能力,特别适用于模板和泛型编程。
388 2
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
3453 3
|
资源调度 Linux 调度
Linux C/C++之线程基础
这篇文章详细介绍了Linux下C/C++线程的基本概念、创建和管理线程的方法,以及线程同步的各种机制,并通过实例代码展示了线程同步技术的应用。
489 0
Linux C/C++之线程基础
|
Linux C++
Linux C/C++之IO多路复用(poll,epoll)
这篇文章详细介绍了Linux下C/C++编程中IO多路复用的两种机制:poll和epoll,包括它们的比较、编程模型、函数原型以及如何使用这些机制实现服务器端和客户端之间的多个连接。
912 0
Linux C/C++之IO多路复用(poll,epoll)
|
网络协议 Linux 网络性能优化
Linux C/C++之TCP / UDP通信
这篇文章详细介绍了Linux下C/C++语言实现TCP和UDP通信的方法,包括网络基础、通信模型、编程示例以及TCP和UDP的优缺点比较。
1088 0
Linux C/C++之TCP / UDP通信
|
消息中间件 Linux API
Linux c/c++之IPC进程间通信
这篇文章详细介绍了Linux下C/C++进程间通信(IPC)的三种主要技术:共享内存、消息队列和信号量,包括它们的编程模型、API函数原型、优势与缺点,并通过示例代码展示了它们的创建、使用和管理方法。
663 0
Linux c/c++之IPC进程间通信