设为首页 加入收藏

TOP

std_set作为一个有序集合(一)
2013-01-01 14:47:57 】 浏览:962
Tags:std_set 作为 一个 有序 集合

 

  在一般意识中,一个集合并没有太多的操作,所有在STL中,std::set也没有太多操作,它的排序是自动的,我们可以插入一个元素,也可以删除一个元素,也有迭代器。下面简单的示例包括std::set自身的多数的特性:

  #include

  int array ={12, 34, 10, 98, 3};

  const size_t array_size=sizeof(array)/sizeof(array[0]);

  //一个新的定义容器的方法

  std::set《/span>int> set(array, array+array_size);

  //插入一个元素

  set.insert(23);

  //移除一个元素

  set.erase(10);

  //移除一个元素不在集合中的元素

  //此时什么也不做,n作为返回值将会是0

  //指明没有元素移除

  size_t n=set.erase(11);

  //www.software8.co

  //使用迭代器,找到一个元素

  std::set《/span>int>::const_iterator result=set.find(98);

  std::set《/span>int> other;

  //交换两个集合的内容

  std::swap(set, other);

  //清楚所有的内容

  other.clear();

  eg:

  #include

  #include

  #include

  using namespace std;

  int main(void)

  {

  std::set _signals;

  _signals.insert(SIGTERM);

  _signals.insert(SIGINT);

  _signals.insert(SIGCHLD);

  for( std::set::const_iterator i = _signals.begin(); i != _signals.end(); ++i)

  cout 《 *i 《 endl;

  _signals.clear();

  return 0;

  }

      

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c++中查询硬件与系统信息api 下一篇C++标准库set类型

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目