STL之集合:set
阿新 • • 發佈:2018-11-10
集合:set
集合是常用的容器。set中的所有元素都會根據元素的建值自動排序,且每個元素最多中出現一次
注意: iterator是迭代器,是STL中的重要概念,類似於指標。
set 中各個函式 |
作用 |
begin() |
返回指向第一個元素的迭代器 |
end() |
返回指向最後一個元素的迭代器 |
count() |
返回某個值的個數 |
empty() |
如果集合為空,返回true |
find() |
返回一個指向被查詢到的元素的迭代器 |
insert() |
在集合中插入元素 |
size() |
返回集合中元素個數 |
swap() |
交換兩個集合變數 |
upper_bound() |
返回大於某個值元素的迭代器 |
#include <iostream> #include <set> using namespace std; int main() { set<int> dict; //整形集合 for(int i = 100 ; i > 0 ; i-- ) { dict.insert(i); //插入資料 } cout << "set含有:" << dict.size() << "個元素" << endl; //集合個數 cout << "5在第" << *dict.find(5) << "個" << endl; //查詢集合中位置5的迭代器 // iterator是一個迭代器,用法類似於指標 for(set<int>::iterator it = dict.begin(); it != dict.end() ; ++it ) { cout << *it << ends; } return 0; }