c++ 容器基本方法

來源:互聯網
上載者:User

標籤:

map

定義map:map<string,int> word_count;

添加新元素:word_count[“hengshan”]=10; 這種方法會尋找,如果找不到直接插入新元素。也可以使用insert,如 word_count.insert(make_pair(“hengshan”,10)),這樣比較正統。

尋找某個元素:使用subscript會有副作用,就是如果元素不存在,那麼會自動建立一個元素,所以用find 比較好。如 int aa=word_count.find(“hengshan”);另外可以使用count,尋找某個元素出現了多少次。使用指標可以,尋找一個元素而不自動增加,如 map<string,int>::iterator it=word_count.find(“hengshan”);?

if(it!=word_count.end()) cout<<it->second();

刪除元素:word_count.erase(“hengshan”)
遍曆:使用 迭代器 iterator
大小  size

set?

大小:size

尋找 find,返回的是 指標類型,iterator

count,尋找有幾個這樣的元素

添加:insert

刪除:erase

set 裡面儲存是按照一定順序的,比如存string 是按照字典順序,和第幾個加到set裡無關。

c++ 容器基本方法

聯繫我們

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