/* set:二叉搜索树维护集合的容器 map:维护键和键对应的值的容器 */ #include<iostream> #include<set> #include<map> #include<string> using namespace std; int main() { set<int> s; s.insert(1); s.insert(5); s.insert(3); set<int>::iterator ite; ite = s.find(1); if (ite == s.end())cout << "not found"; else cout << "found"; //删除元素 s.erase(1); //其它查找方式 if (s.count(3) != 0)cout << "found"; else cout << "not found"; //遍历 for (ite = s.begin(); ite != s.end(); ite++) cout << *ite << " "; map<int, const char*> m; m.insert(make_pair(1, "one")); m.insert(make_pair(10, "ten")); m[100] = "hundred"; map<int, const char*>::iterator ite2; ite2 = m.find(1);//查找 cout << ite2->second << endl; ite2 = m.find(2); if (ite2 == m.end())cout << "not found"; else cout << ite2->second; cout << m[10];//其它写法 m.erase(10);//删除 //遍历 for (ite2 = m.begin(); ite2 != m.end(); ite2++) cout << ite2->first << " " << ite2->second << endl; system("pause"); return 0; }
原文:http://www.cnblogs.com/littlehoom/p/3513921.html