---
title: "C++ Map Source"
author: "vergilwang"
date: "2012-12-21T11:39:00Z"
tags: []
categories: ["编程语言"]
slug: "c-map-source"
draft: false
lastmod: "2014-02-15T15:49:13Z"
id: 2011297
url: "https://vergilwang.iteye.com/blog/2011297"
---

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

readtohash(both, &both\_map);  
readtohash(neg, &neg\_map,1);  
readtohash(pos, &pos\_map,1);  
cout&lt;&lt;"The Both Map is:"&lt;&lt;both\_map.size()&lt;&lt;endl;  
cout&lt;&lt;"The Neg Map is:"&lt;&lt;neg\_map.size()&lt;&lt;endl;  
cout&lt;&lt;"The Pos Map is:"&lt;&lt;pos\_map.size()&lt;&lt;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 &lt;iostream&gt;  
#include &lt;iomanip&gt;  
#include &lt;fstream&gt;  
#include &lt;map&gt;  
using namespace std;  
void readtohash(char \*path, map&lt;string, int&gt; \*hash\_map) {  
//char buffer\[256];  
ifstream myfile (path);  
if (!myfile) {  
cout&lt;&lt; "Unbale to open"&lt;&lt;endl;  
}  
while (myfile.peek() != EOF) {  
string str;  
int sign = -5;  
myfile&gt;&gt;str&gt;&gt;sign; //ch is for /n  
(\*hash\_map)\[str] = sign; // the privilege of square bracket\[] if lower than *  
//cout&lt;&lt;str&lt;&lt;sign&lt;&lt;endl;

}  
myfile.close();  
}

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

}  
myfile.close();  
}  
void merge(map&lt;string, int&gt; \*both\_map, map&lt;string, int&gt; \*neg\_map, map&lt;string, int&gt; \*pos\_map) {  
map&lt;string, int&gt;::iterator it;  
it = (\*both\_map).begin();  
while (it != (\*both\_map).end()) {  
//cout&lt;&lt; it-&gt;first&lt;&lt;";";  
//cout&lt;&lt; it-&gt;second&lt;&lt;endl;  
if (it-&gt;second == 1)  
{  
(\*neg\_map).erase(it-&gt;first);  
}  
else if(it-&gt;second == -1)  
{  
(\*pos\_map).erase(it-&gt;first);  
}  
else {  
(\*neg\_map).erase(it-&gt;first);  
(\*pos\_map).erase(it-&gt;first);  
}  
it++;  
}  
}  
void writefromhash(char* path, map&lt;string, int&gt; \*hash\_map) {  
map&lt;string, int &gt;::iterator it;  
it = (\*hash\_map).begin();  
ofstream outfile (path);  
while (it != (\*hash\_map).end()) {  
outfile&lt;&lt;it-&gt;first&lt;&lt;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 &lt;map&gt;  
#include &lt;string&gt;  
using namespace std;  
void readtohash(char \*path, map&lt;string, int&gt; \*hash\_map);  
void readtohash(char \*path, map&lt;string, int&gt; \*hash\_map, int value);  
void merge(map&lt;string, int&gt; \*both\_map, map&lt;string, int&gt; \*neg\_map, map&lt;string, int&gt; \*pos\_map);  
void writefromhash(char \*path, map&lt;string, int&gt; \*hash\_map);  
#endif