2018/09/11 上午之set.map简单学习


/*
这里介绍RB-TREE实现的set,由于RB-TREE是平衡二叉搜索树,所以set具有自动排序的功能。
stl的1.find()函数与set关联容器自带的2.find()的使用方法及比较。
21优


set<int> iset;
set<int>::iterator it;

1.stl之find 循环比较实现,O(n)
it = find(iset.begin(),iset.end(),val);
if(it != iset.end() ) cout << "found" << endl;
else cout << "not found" << endl; //(it == iset.end() )

2.set自带的find o(logn)
it = iset.find(val);
if(it != iset.end() ) cout << "found" << endl;
else cout << " not found " << endl; // (it == iset.end() ) 

所以推荐使用2.


map的底层也是用RB-TREE实现的。所以key也是自动排好序的。
map的遍历
map<string, int> simap;
pair<string, int> val(string("111"), 1);
simap.insert(val);

map<string, int>::iterator it;
for(it=simap.begin(); it!=simap.end(); it++)
{
    cout << it->first << ' ';
    cout << it->second << endl;
}

map 的find()操作
map<string,int>::iterator it1;
it1 = simap.find(key);
if(it != simap.end() ) cout << "found" <<endl;
else cout << "not found " <<endl; //(it == simap.end() )
其实同set的find()是一样的
*/

全部评论

相关推荐

程序员花海:实习和校招简历正确格式应该是教育背景+实习+项目经历+个人评价 其中项目经历注意要体现业务 实习经历里面的业务更是要自圆其说 简历模板尽可能保持干净整洁 不要太花哨的
秋招吐槽大会
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务