C++ Map Source

/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012年12月18日 13时29分04秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include<iostream>
#include "main.h"
#include <string>
#include <map>
using namespace std;
int main()
{
char both[255] = "./both.txt";
char neg[255] = "./negative.txt";
char pos[255]= "./positive.txt";
map<string, int> both_map;//map is a variable, but not a pointer
map<string ,int> neg_map;
map<string ,int> pos_map;


readtohash(both, &both_map);
readtohash(neg, &neg_map,1);
readtohash(pos, &pos_map,1);
cout<<"The Both Map is:"<<both_map.size()<<endl;
cout<<"The Neg Map is:"<<neg_map.size()<<endl;
cout<<"The Pos Map is:"<<pos_map.size()<<endl;
merge(&both_map, &neg_map, &pos_map);
writefromhash("./new_negative.txt", &neg_map);
writefromhash("./new_positve.txt", &pos_map);


}


/*
 * =====================================================================================
 *
 *       Filename:  main.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012年12月18日 16时54分59秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
using namespace std;
void readtohash(char *path, map<string, int> *hash_map) {
//char buffer[256];
ifstream myfile (path);
if (!myfile) {
cout<< "Unbale to open"<<endl;
}
  while (myfile.peek() != EOF) {
string str;
int sign =  -5;
myfile>>str>>sign; //ch is for /n
(*hash_map)[str] = sign; // the privilege of square bracket[] if lower than *
// cout<<str<<sign<<endl;

}
myfile.close();
 }


void readtohash(char *path, map<string, int> *hash_map, int value) {
ifstream myfile (path);
if (!myfile) {
cout<< "Unbale to open"<<endl;
}
  while (myfile.peek() != EOF) {
string str;
myfile>>str;
(*hash_map)[str] = value; // the privilege of square bracket[] if lower than *

}
myfile.close();
 }
void merge(map<string, int> *both_map, map<string, int> *neg_map, map<string, int> *pos_map) {
map<string, int>::iterator it;
it = (*both_map).begin();
while (it != (*both_map).end()) {
// cout<< it->first<<";";
// cout<< it->second<<endl;
if (it->second == 1)
{
(*neg_map).erase(it->first);
}
else if(it->second == -1)
{
(*pos_map).erase(it->first);
}
else {
(*neg_map).erase(it->first);
(*pos_map).erase(it->first);
}
it++;
}
}
void writefromhash(char* path, map<string, int> *hash_map) {
map<string, int >::iterator it;
it = (*hash_map).begin();
ofstream outfile (path);
while (it != (*hash_map).end()) {
outfile<<it->first<<endl;
it++;
}
}


/*
 * =====================================================================================
 *
 *       Filename:  main.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012年12月18日 13时28分06秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#ifndef _main_h
#define _main_h
#include <map>
#include <string>
using namespace std;
void readtohash(char *path, map<string, int> *hash_map);
void readtohash(char *path, map<string, int> *hash_map, int value);
void merge(map<string, int> *both_map, map<string, int> *neg_map, map<string, int> *pos_map);
void writefromhash(char *path, map<string, int> *hash_map);
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值