Problem:
Reads a piece of text into a vector object, and each word is stored as an element in the vector. Put the Vector object
Each word is converted to uppercase. Outputs the converted elements in the vector object, with each 8 words being one line of output.
Code:
1#include <iostream>2#include <vector>3 4 using namespacestd;5 6 intMain ()7 {8cout<<"Enter a text (Ctrl + Z end):"<<Endl;9vector<string>Vecstr;Ten stringWord; One while(cin>>word) A { - Vecstr.push_back (word); - } the if(vecstr.size () = =0) - { -cout<<"No input string"<<Endl; - return-1; + } - for(vector<string>::size_type i =0; I < vecstr.size (); i++) + { A for(string:: Size_type j =0; J < Vecstr[i].size (); J + +) at { -VECSTR[I][J] =ToUpper (Vecstr[i][j]); - } -cout<<vecstr[i]<<" "; - if((i+1) %8==0) - { incout<<Endl; - } to } + return 0; -}
View Code
Summarize:
(1) Vector<type>,string method size Returns a value of type Vector<type>::size_type and String::size_type;
(2) Read input data multiple times using while (CIN>>STR) {};
(3) to determine whether the vector is empty, good programming habits;
(4) The method of converting the case with string, ToUpper (c), ToLower (c);
(5) Use ((i + 1)% 8) = = 0 To judge the line, form concise.
C + + Primer (fourth edition) after class exercise 3.14