Pin-Jiun / Programming-Language-CPP

It's the note/experience about C++, and I have the basic ability of C.
0 stars 0 forks source link

17-STL Container set #21

Open Pin-Jiun opened 1 year ago

Pin-Jiun commented 1 year ago

set

集合,去除重複的元素,資料由小到大排序。 #include <set>

創建

  set<int> first{1,2,3};
  int myints[]= {10,20,30,40,50};
  set<int> second (myints,myints+5);        // range
  set<int> third (second);                  // a copy of second
  set<int> fourth (second.begin(), second.end());  // iterator

Iterators

begin() end() 回傳一個 iterator,它指向 vector 最尾端元素的下一個位置(請注意:它不是最末元素) rbegin() rend() cbegin() cend() crbegin() crend()


Capacity

empty() size() max_size


Capacity

insert() erase() clear()


Operations

find() Searches the container for an element equivalent to val and returns an iterator to it if found, otherwise it returns an iterator to set::end. iterator find (const value_type& val);

count()

lower_bound() upper_bound()

  for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90

  itlow=myset.lower_bound (30);                //       ^
  itup=myset.upper_bound (60);                 //                   ^

  myset.erase(itlow,itup);                     // 10 20 70 80 90