1.将存储在一个向量中的所有单词打印出来(code in C++)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc,char** argv)
{
char* cword[]={"After","Hitting","His","'Peak'","In","1997","A","Manager","Shares","What","He","Has","Learned"};
vector<string> words(cword,cword+13);
vector<string>::iterator iter=words.begin();
bool first_word=true;//print space between words excepts for the first word!
while(iter!=words.end())
{
if(first_word)
first_word=false;
else
cout<<" ";
cout<<*iter;
++iter;
}//end of while
cout<<endl;
return 0;
}
2.决定是否打印单词复数形式的方法
string make_plural(size_t ctr,const string &word,const string& ending)
{
return ctr==1?word:word+ending;
}
调用:make_plural(count,word_sample,s);