程序中用到的几个小技巧

 

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);

 

 


posted @ 2013-09-11 18:49  麦苗不是草  阅读(127)  评论(0)    收藏  举报