Saturday 15 March 2014

c++ - Copying strings from vector is not working -


I am trying to copy a word in a file if there is no sound in that word, it is my attempt It's not working, but it's not working. It copies the words into the file but it does not exclude words with vowels. I'm not sure why this does what it does anyway ...

  #include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Sstream & gt; # Include & lt; Cctype & gt; # Include & lt; Fstream & gt; # Include & lt; Vector & gt; Template & lt; Typename it & gt; Bool has_vowel (start it, end it) {for (auto it = start; it! = End; ++ this) {char less = std :: tolower (* this); If (lower == 'A' || lower == 'e' = lower == 'I' || Lower == 'O' = Lower == 'U') is true; } return false; } Int main () {std :: fstream in ("in.txt"); Std :: fstream out ("out.txt"); Std :: vector & lt; Std :: string & gt; V; Std :: string str; While (in & gt; & gt; str) {v.push_back (str); } (This is automatically = V. bijin (); it! = V.end (); ++ this) {if (havvell (it-> start (), it- & gt; and ())   

in.txt

Hello friends and family < / P>

To output out

myllofriendsandfamily

Unless you are feeling masochistic, it is almost find_first_of :

  struct has_vowel {bool operator () ( Std :: string const & amp; a) using static Area is very easy Const std :: string tone ( "Aiaiyueaaiuuuuuuai"); Return a.find_first_of (voice)! = Std :: string :: npos; }};   

When you want to copy some containers, but you want to exclude items from a condition, you can usually go to std :: remove_copy_if Want to use. Since it can work directly with istream_iterator and ostream_iterator , you do not need to store all the words in the vector if you do a job: std :: remove_copy_if (std :: istream_iterator (in), std :: istream_iterator (), std :: Ostream_iterator & lt; std :: string & gt; (outside,), haswewel ());

If you are ready to use C ++ 11, you can use lambda for the condition:

  std :: Remove_copy_if (std :: Std :: string & gt; (), std :: ostream_iterator & lt; std :: string & gt; (outside, ""), [] (std :: string const & gt; std :: string const & gt; std :: string & gt; (in), std :: istream_iterator> S) {return s.find_first_of ("aeiouAEIOU")! = Std :: string :: npos;}) ;    

No comments:

Post a Comment