The CL in this article refers to the C + + language compiler in Visual Studio 2010, which refers to the g++ 4.9 compiler (g++). The getline in CL is the fetch line without "\ n", and the getline in g++ refers to the "\ n" line in the fetch. Here's a detailed description of why I noticed the problem and the code that tested the different platforms. 1 Problems Arise
I followed the book "C + + Primer" 1 in the 11.3.6 section A Word transformation Map program found that the run results are not correct, I used the g++ 4.9.2 compiler. The code is as follows:
A Word Transformation map #include <map> #include <string> #include <fstream> #include <sstream>
;
#include <iostream> using namespace std; Map<string, string> buildmap (Ifstream &map_file) {map<string,string> trans_map;//holds the transform
ations string key; String value; Phrase to use instead//Read the The "the" into key and the rest of the ' line into value while (Map_file >> ;
Key && Getline (Map_file,value)) {if (Value.size () >1)//Check that there is a transformation Trans_map[key] = value.substr (1);
Skip leading spaces else throw runtime_error ("No rule for" + key);
return trans_map; Const string& Transform (const string &s, const map<string, string> &m) {//the actual map work;
This was the heart of the program auto Map_it = M.find (s); If this word was in the transformation map if (Map_it!= m.cend ()){return map_it->second;
else return s;
} void Word_transform (Ifstream &map_file, Ifstream &input) {Auto Trans_map = Buildmap (map_file);
string text;
while (Getline (Input,text)) {//read a line of Inpu Istringstream Stream (text);
string Word;
bool FirstWord = true;
while (stream >> word) {if (FirstWord) FirstWord = false;
else cout << "";
cout << transform (WORD,TRANS_MAP);
} cout << Endl;
int main () {Ifstream in ("WordTrans.txt");
Ifstream inputfile ("WordTransInput.txt");
Word_transform (in, inputfile);
return 0; }
The results obtained by using the g++ 4.9.2 compiler compile run are as follows:
And the results of using CL run are as follows:
I'm worried that g++ can't get the right output. I think cout in the output when there is no flush buffer, so flush command used, ends command also used, but can not solve the problem. VC So god, you can output the correct results. G++ is also a good compiler, why not output the correct results. 2 Analysis Code
After careful analysis of the code, I attributed the crux of the problem to the getline above the function Buildmap (). I changed the 3 cout in the function word_transform () to output to a file, and the result is as follows:
It is easy to see that the Getline in Buildmap () adds an "\ n" (line break) at the end of each line read. So, find out where the problem is, put the above code Trans_map[key] = VALUE.SUBSTR (1);
Trans_map[key] = value.substr (1, Value.size ()-2);
Can. Which is to remove the last line break. At this point, the program can get the correct running results in g++. Why the output to the file and output to the screen is different before the code changes, which involves a buffer problem. The reason I just started using the flush buffer was that getline was at work and didn't find the source.
But the problem is again, the resulting code in CL results in an abnormal display:
It is clear from the above that each translated word is missing the last letter. It can be concluded that getline in CL without the "\ n" character. 3 Test Platform getline semantics of small code
As follows:
This are used to test the
//getline semantic for g++ or VC
#include <iostream>
#include <str ing>
#include <fstream>
using namespace std;
int main () {
ifstream infile ("GetlinePtest.txt");
String value;
Getline (infile,value);
cout << value;
Getline (infile,value);
cout << value;
}
The contents of the GetlinePtest.txt file are:
I Love china!
I Love beijing!
[Blank line]
Output "I Love china! I Love beijing! " For CL compiler, output "I love china!" For the g++ compiler, because "I love beijing!" was flushed off by the system command prompt. 4 Concluding remarks
Getline has different semantics on the CL and g++ compilers. If you do not recognize this, you will not be able to correctly understand the output of the program. So be careful when using the Getline function. Stanley B. Lippman, Josee Lajoie, Barbara E. Moo. C + + Primer. 5th ed. Upper Saddle River NJ, Addison-wesley, 2013. ↩