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 }
有一个更简单的等价的下面的代码:
myset.find(x) != myset.end()
const bool is_in = container.find(element) != container.end();
原文:https://www.cnblogs.com/zle1992/p/10168504.html