Strange behavior in C++ while loop and ifstream -


So I have an issue where my functions will not work properly when I read an input from a file in a loop x I:

  ifstream in (inputFileName.c_str ()); // Input file is a string string word; While (in & gt; & gt; terms) {cout & lt; & Lt; The word & lt; & Lt; Endl; // This behavior because this file should print all words in test.insert (word, 0); // This function will not include words! }  

But if I do

  in & gt; & Gt; word; Test.insert (word, 0); In & gt; & Gt; word; Test.insert (word, 0); In & gt; & Gt; word; Test.insert (word, 0);  

.... all this works fine !! This is really strange to me, any thoughts that may be due to it? I think the problem is probably in your hash table class, from which test < / Code> is an example.

Otherwise, lack of a great reason to do this, I should at least std :: unordered_set instead:

  std :: Ifstream in (inputFilename); // `.c_str () 'is no longer required to read in C + + 11/12 std :: unordered_set test {std :: istream_iterator & lt; Std :: string & gt; (In), std :: istream_iterator & lt; Std :: string & gt; ()}; // Display unique words: std :: cout & lt; & Lt; "Unique word: \ n"; Std :: copy (test.begin (), test.end (), std :: ostream_iterator & lt; std :: string & gt; (std :: cout, "\ n"));  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -