uva 10815 set的用法

來源:互聯網
上載者:User

本題主要學習的就是set的用法。。也知道set中元素有著從小到大排好序的性質。。

代碼,詳情見注釋

#include<iostream>#include<cctype>#include<set>#include<string>#include<sstream>using namespace std;set<string> dict;int main (){string s,buf;while (cin >> s){int i;for(i=0;i<s.size();i++){if(isalpha(s[i])) //判斷是否為字母  類似的函數還有isupper islower isdigit s[i]=tolower(s[i]);      //將字母轉化成小寫   類似的還有toupper else s[i]=' '; //此處將不是字母的都轉化為空白格 而stringstream會濾掉空格 最後只會剩下單詞 }stringstream ss(s);//ss>>buf;    不能寫成這樣  因為有可能在單詞前面有標點 而在上面被轉化成了空格就無法讀入單詞,還是應該用while將流中的東西讀乾淨~ while(ss >> buf)dict.insert(buf);     //插入到set中 set保證裡面的元素不重複 }set<string>::iterator it;   //此處為STL中的迭代器。。 要記住用法.begin()  .end() for(it=dict.begin();it!=dict.end();it++)cout << *it << endl;return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.