c++ set 的常见操作

本文介绍了C++中set容器的基本操作,包括begin()、end()、clear()、insert()、count()、empty()、erase()、find()、size()、swap()、max_size()、lower_bound()、upper_bound()、rbegin()和rend()。这些操作涵盖了插入、删除、查询、遍历等set的主要功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • begin()

iterator begin();
const_iterator begin()const;

返回引用set容器中第一个元素的迭代器。

由于set容器始终保持其元素有序,因此起点指向遵循容器的排序标准的第一个元素。

如果容器为,则不应取消对返回的迭代器值的引用。

  • end()

iterator end();
const_iterator end()const;

返回一个迭代器,该迭代器引用set容器中的past-the-end元素。 在过去的最末端元素是会跟随的最后一个元素中的理论元素容器。它没有指向任何元素,因此不应取消引用。 由于标准库函数使用的范围不包括其闭合迭代器指向的元素,因此该函数通常与set :: begin结合使用以指定包含容器中所有元素的范围。 如果容器为,则此函数返回与set :: begin相同的

示例

 

#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {75,23,65,42,13};
  set<int> myset (myints,myints+5);

  cout << "myset contains:";
  for (set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    cout << ' ' << *it;

 cout << '\n';

  return 0;
}
  • void clear();

移除set中所有元素,使得容器的大小为0;

  • intsert()

一个参数
pair<iterator,bool> insert (const value_type& val);
指定迭代器位置插入
iterator insert (iterator position, const value_type& val);
范围插入
template <class InputIterator>
  void insert (InputIterator first, InputIterator last);

示例

// set::insert (C++98)
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;
  pair<set<int>::iterator,bool> ret;

  // set some initial values:
  for (int i=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50
  ret = myset.insert(20);               // no new element inserted

  if (ret.second==false) it=ret.first;  // "it" now points to element 20

  myset.insert (it,25);                 // max efficiency inserting
  myset.insert (it,24);                 // max efficiency inserting
  myset.insert (it,26);                 // no max efficiency inserting

  int myints[]= {5,10,15};              // 10 already in set, not inserted
  myset.insert (myints,myints+3);

  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';
  myset.clear();						//clear all elements
  int myintss[]={5,6,7,8};					
  myset.insert(myintss,myintss+4) ;       // intsert new elements
  cout << "myset contains:";
  for(it=myset.begin();it!=myset.end();++it)
  		cout << ' ' << *it;
  cout << '\n';
  return 0;
}
  •  count()

size_type count (const value_type& val) const;

查找 val 是否在容器 中 如果在返回1 否则返回0 ;

// set::count
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  set<int> myset;

  // set some initial values:
  for (int i=1; i<5; ++i) myset.insert(i*3);    // set: 3 6 9 12

  for (int i=0; i<10; ++i)
  {
    cout << i;
    if (myset.count(i)!=0)
      cout << " is an element of myset.\n";
    else
      cout << " is not an element of myset.\n";
  }

  return 0;
}

 

  • empty()

bool empty()const;

返回设置的容器是否为空(即其大小是否为0)。

此功能不会以任何方式修改容器。要清除设置容器的内容,请参见set :: clear

例 

// set::empty
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  set<int> myset;

  myset.insert(20);
  myset.insert(30);
  myset.insert(10);

  cout << "myset contains:";
  while (!myset.empty())
  {
     cout << ' ' << *myset.begin();
     myset.erase(myset.begin());
  }
  cout << '\n';

  return 0;
}

 

  • erase()

(1)
     void erase (iterator position);
(2)
size_type erase (const value_type& val);
(3)
     void erase (iterator first, iterator last);

 从set容器中移除单个元素或一系列元素([first,last))。

这有效地减少了容器尺寸,减少了被破坏的元素的数量。

参数:

位置

指向要从集合中删除的单个元素的迭代器。
成员类型迭代器和const_iterator是指向元素的双向迭代器类型。

要从集合中删除的
构件类型VALUE_TYPE是在容器中的元件,在所定义的类型集合作为其第一个模板参数(的别名Ť)。

first,last

迭代器,它指定要删除的set容器内的范围:[first,last)。即,范围包括firstlast之间的所有元素,包括first指向的元素,但last指向不包含的元素。
成员类型迭代器和const_iterator是指向元素的双向迭代器类型。

// erasing from set
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

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

  it = myset.begin();
  ++it;                                         // "it" points now to 20

  myset.erase (it);

  myset.erase (40);

  it = myset.find (60);
  myset.erase (it, myset.end());

  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}
  •  find()

iterator (const value_type&val)const;

返回寻找的元素值的迭代器,如果没找到返回end()

// set::find
#include <iostream>
#include <set>
using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  // set some initial values:
  for (int i=1; i<=5; i++) myset.insert(i*10);    // set: 10 20 30 40 50

  it=myset.find(20);
  myset.erase (it);
  myset.erase (myset.find(40));

  cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

 

  • size()

size_type size() const;

返回集合中元素的个数

// set::size
#include <iostream>
#include <set>
using namespace std;
 
int main ()
{
  set<int> myints;
  cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<10; ++i) myints.insert(i);
  cout << "1. size: " << myints.size() << '\n';

  myints.insert (100);
  cout << "2. size: " << myints.size() << '\n';

  myints.erase(5);
  cout << "3. size: " << myints.size() << '\n';

  return 0;
}
  •  swap

void swap (set& x);

交换俩个集合中的元素

 

// swap sets
#include <iostream>
#include <set>
using namespace std;
main ()
{
  int myints[]={12,75,10,32,20,25};
  set<int> first (myints,myints+3);     // 10,12,75
  set<int> second (myints+3,myints+6);  // 20,25,32

  first.swap(second);

  cout << "first contains:";
  for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  cout << "second contains:";
  for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

 

  •  max_size()

   返回该集合的最大容量

 

  • lower_bound()

  • upper_bound()

iterator lower_bound (const value_type& val) const;

返回关键值的下界与上界

// set::lower_bound/upper_bound
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator itlow,itup;

  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 (30);                 //                   ^

  myset.erase(itlow,itup);                     // 10 20 70 80 90
  std::cout<<*itlow<<" "<<*itup;
  std::cout << "myset contains:";
  for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

 

  • rbegin() 

  • rend()

搭配使用可以逆序输出集合的元素

// set::rbegin/rend
#include <iostream>
#include <set>

int main ()
{
  int myints[] = {21,64,17,78,49};
  std::set<int> myset (myints,myints+5);

  std::set<int>::reverse_iterator rit;

  std::cout << "myset contains:";
  for (rit=myset.rbegin(); rit != myset.rend(); ++rit)
    std::cout << ' ' << *rit;

  std::cout << '\n';

  return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值