顺序容器的使用C++

来源:互联网 发布:删除数据库用户 编辑:程序博客网 时间:2024/06/11 15:07

   有关顺序容器的使用,觉得可以拿出来,值得拿出来的东西,写起来也不是那么简单.

  刚刚去软考报名回来,软件设计师,又要开始了.一次小小的旅行,美丽的世界,等着我吧.

  贴咯,不多说.

// 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 ;}}
原创粉丝点击