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

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

 

时间: 2024-07-30 21:47:28

linux下练习 c++ 关联式容器map特性的相关文章

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

/* multimap特性 key可以重复 不支持下标访问 */ #include<iostream> #include<string> #include "print.h" #include<map> using namespace std; typedef pair<int,string> pairmp; typedef multimap<string,double> MS; int main() { MS m; m.in

linux下练习 c++ 关联式容器共性测试,使用

/* 关联式容器共性:二叉查找树实现,自动根据关键字排序,自动平衡 set<K>,multiset<K>,map<K,V>,multimap<K,V> 查找:.find(key) 失败返回.end() 统计:.count(key) 删除:.erase(key) 插入:.insert(element) 区间:.lower_bund(key) //取得关键字为key的第一个元素位置 .upper_bound(key) //取得关键字为key的最后一个元素之后的位

linux下练习 c++ 类库中list的特性、关联式容器共性介绍

/* 库模版中 list 特性: 双向链表 增删:.push_front(element),.pop_front(),.remove(element) 不支持下标访问 除去重复:.unique() 相邻的重复元素只保留一个 排序:.sort(),默认用'<'号比较,自定义类型要重载运算符 倒置:.reverse() 转移:.aplice(pos,list2),.aplice(pos,list2,pos2), .aplice(pos,list2,pos2_begin,pos2_end) 归并:.m

linux下Sersync实现触发式文件同步

序言:如果我们后端有多台网站服务器或者文件服务器,而且没有好的文件同步机制,那么当我们升级程序或者更新文件的时候,就需要每台服务器或者目录都要更新,这样很容易出问题,并很容易导致两边的文件不一致,从而出现很多莫民其妙的问题.因此我们需要使用好的文件同步方式来实现几个后端服务器文件的同步,目前广泛采用的方案是使用rsync+inotify的方式来实现文件的触发更新.原理是采用inotify来对文件进行监控,当监控到文件有文件发生改变的时候,就会调用rsync实现触发式实时同步!本文就来详细介绍金山

linux下练习 c++ 序列容器的使用

//sequence.cpp   // sequence.cpp /* 序列式容器:vector,deque,list 插入:.insert(position,n,element), .insert(position,pos_begin,pos_end) 赋值:.assign(n,element), .assign(pos_begin,pos_end) 调整:.resize(n,element=value) 首尾:.front(),.back() 增删:.push_back(element),

如何在linux下检测内存泄漏

1.开发背景 在 windows 下使用 VC 编程时,我们通常需要 DEBUG 模式下运行程序,而后调试器将在退出程序时,打印出程序运行过程中在堆上分配而没有释放的内存信息,其中包括代码文件名.行号以及内存大小.该功能是 MFC Framework 提供的内置机制,封装在其类结构体系内部. 在 linux 或者 unix 下,我们的 C++ 程序缺乏相应的手段来检测内存信息,而只能使用 top 指令观察进程的动态内存总额.而且程序退出时,我们无法获知任何内存泄漏信息.为了更好的辅助在 linu

如何在linux下检测内存泄漏(转)

  本文转自:http://www.ibm.com/developerworks/cn/linux/l-mleak/ 本文针对 linux 下的 C++ 程序的内存泄漏的检测方法及其实现进行探讨.其中包括 C++ 中的 new 和 delete 的基本原理,内存检测子系统的实现原理和具体方法,以及内存泄漏检测的高级话题.作为内存检测子系统实现的一部分,提供了一个具有更好的使用特性的互斥体(Mutex)类.   1.开发背景 在 windows 下使用 VC 编程时,我们通常需要 DEBUG 模式

linux下创建线程内存泄漏,php的json

  这次还是把遇到的几个问题整理一下,希望再遇到的同学能轻松解决.另外最近博客的feeds延迟更新的原因也会一起说明一下. 1.linux下创建线程导致内存泄漏 今天在外网发布了一个server之后,用top发现virt的使用量一直在涨,而且一次涨8m.于是可以断定有内存泄漏了,经过排查,最终确定原因出在多线程的问题上: 代码如下: 1 2 3 4 5 6 pthread_t thread_id; int ret=pthread_create(&thread_id, NULL, flush_th

Linux下c++编译器Code::Blocks安装

最近想写写C .C++方面的程序,所以想找一个Linux下的编辑器来用用, 找了很多也试了不少,最后锁定了CodeBlocks.以下是关于他的介绍和一些安装 过程.适用所有的Linux吧.(有时就只是需要一个安装的思路,其它的都差不 多一样的) (摘抄)Code::Blocks,有时也简单打成 "CodeBlocks",是一款全新的C++集成设置环境软件(IDE). 作为一款C++的IDE,Code::Blocks至少有以下几点非常吸引我. 开源--开源不仅仅意味着免费,但就算是仅仅是