順序容器的使用C++

來源:互聯網
上載者:User

   有關順序容器的使用,覺得可以拿出來,值得拿出來的東西,寫起來也不是那麼簡單.

  剛剛去軟考報名回來,軟體設計師,又要開始了.一次小小的旅行,美麗的世界,等著我吧.

  貼咯,不多說.

// 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 ;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.