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
總結
以上是生活随笔為你收集整理的C++ Map Source的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Map传递参数
- 下一篇: QuickFix