二叉搜索树(binary search tree) 集合(set)和映射(map) 代码(C++)
本文地址: http://blog.csdn.net/caroline_wendy
二叉搜索树(binary search tree)作为常用而高效的数据结构, 标准库中包含实现, 在标准库的集合(set)和映射(map), 均使用.
具体操作代码如下.
代码:
/* * main.cpp * * Created on: 2014.7.20 * Author: spike */ /*eclipse cdt, gcc 4.8.1*/ #include <stdio.h> #include <set> #include <map> using namespace std; int main(void) { set<int> s; s.insert(1); s.insert(3); s.insert(5); set<int>::iterator ite; ite = s.find(1); if (ite == s.end()) puts("not found"); else puts("found"); ite = s.find(2); if (ite == s.end()) puts("not found"); else puts("found"); s.erase(3); if (s.count(3) != 0) puts("found"); else puts("not found"); for (ite=s.begin(); ite!=s.end(); ++ite) { printf("%d ", *ite); } printf("\n"); 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 mite; mite = m.find(1); puts(mite->second); mite = m.find(2); if (mite == m.end()) puts("not found"); else puts(mite->second); puts(m[10]); m.erase(10); for (mite = m.begin(); mite!=m.end(); ++mite) { printf("%d: %s; ", mite->first, mite->second); } printf("\n"); return 0; }
输出:
found not found not found 1 5 ONE not found TEN 1: ONE; 100: HUNDRED;
时间: 2024-11-03 22:14:03