码迷,mamicode.com
首页 > 编程语言 > 详细

c++ 常用的数据结构

时间:2018-12-24 16:18:34      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:col   str   tle   struct   system   print   元素   nta   stl容器   

set

 

 

 1 // constructing sets
 2 #include <iostream>
 3 #include <set>
 4 
 5 void checkin(std::set<int> & myset,int v)
 6 {
 7     if (myset.find(v) != myset.end())
 8         std::cout << "in " << std::endl;
 9     else
10         std::cout << "not" << std::endl;
11 };
12 int main()
13 {
14 
15     int myints[] = { 10, 20, 30, 40, 50 };
16     std::set<int> myset(myints, myints + 5);        // range
17 
18     checkin(myset, 10);
19     
20     myset.erase(myset.find(10));//删除元素
21     checkin(myset, 10);
22     myset.insert(10);//添加元素
23     checkin(myset, 10);
24 
25     system("pause");
26     return 0;
27 }

 

c++ 如何检查一个元素是否在std :: set?

有一个更简单的等价的下面的代码:

 myset.find(x) != myset.end()
检查许多STL容器中的存在的典型方法是:
const bool is_in = container.find(element) != container.end();
 

c++ 常用的数据结构

标签:col   str   tle   struct   system   print   元素   nta   stl容器   

原文地址:https://www.cnblogs.com/zle1992/p/10168504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!