有關順序容器的使用,覺得可以拿出來,值得拿出來的東西,寫起來也不是那麼簡單.
剛剛去軟考報名回來,軟體設計師,又要開始了.一次小小的旅行,美麗的世界,等著我吧.
貼咯,不多說.
// 9-39-2011-09-03-10.09.cpp -- 第九章第三十九題#include "stdafx.h"#include <iostream>#include <string>#include <vector>using namespace std ;const unsigned int Infinity = 1 << 31 ;void processStringAndPrintStatisticResult (const string & str, const string & separators) ;int _tmain(int argc, _TCHAR* argv[]){string separators(",:\v\r\t\n?!. ") ;string line1 = "We were her pride of 10 she names us:" ;string line2 = "Benjamin, Phoenix, the Prodigal" ;string line3 = "and perspicacious pacific Suzanne" ;string sentence = line1 + ' ' + line2 + ' ' + line3 ;processStringAndPrintStatisticResult(sentence, separators) ;return 0 ;}void processStringAndPrintStatisticResult (const string & str, const string & separators){string ::size_type numOfWords = 0 ;vector<string> longestWords ;vector<string> shortestWords ;string ::size_type maxLength = 0 ;string ::size_type minLength = Infinity ;string ::size_type letterStartPos = 0 ;string ::size_type separatorStartPos = 0 ;string word ;while ((letterStartPos = str.find_first_not_of(separators, letterStartPos)) != string ::npos){separatorStartPos = str.find_first_of(separators, letterStartPos) ;//Calculate length of word.++numOfWords ;//Extract the word has been found just now.if (separatorStartPos != string ::npos){word.assign(str.begin() + letterStartPos, str.begin() + separatorStartPos) ;}else{word.assign(str.begin() + letterStartPos, str.end()) ;}if (word.size() > maxLength){maxLength = word.size() ;longestWords.clear() ;longestWords.push_back(word) ;}else if (word.size() == maxLength){longestWords.push_back(word) ;}if (word.size() < minLength){minLength = word.size() ;shortestWords.clear() ;shortestWords.push_back(word) ;}else if (word.size() == minLength){shortestWords.push_back(word) ;}//Updata letterStartPos.letterStartPos = separatorStartPos ;}cout << "Number of words is : " << numOfWords << endl ;cout << "All maximum length words:" << endl ;vector<string> ::const_iterator iter = longestWords.begin() ;while (iter != longestWords.end()){cout << *iter << endl ;++iter ;}cout << "All minimum length words:" << endl ;iter = shortestWords.begin() ;while (iter != shortestWords.end()){cout << *iter << endl ;++iter ;}}