poj 2418 Hardwood Species --- 再来复习遍map吧
来源:互联网 发布:数据录入员有前途吗 编辑:程序博客网 时间:2024/06/15 19:32
题目是统计字符串出现的频率,应该是用二分排序树做的吧。
这里 8000多ms。。
输入输出纠结死我了。。。
再记一遍
输入:
map<int, string> mapStudent;
1、mapStudent.insert(pair<int, string>(1, “student_one”));
2、mapStudent.insert(map<int, string>::value_type (2, “student_two”));
3、mapStudent[3] = “student_three”;
前两种方法,在出现重复的值时不能使用,而第三种可以直接覆盖。
遍历:
1、利用前向迭代器
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout<<iter->first<<” ”<<iter->second<<end;
↓ ↓
map< int, string>
2、利用反向迭代器
map<int, string>:::reverse_iterator iter; ↓ 这里有r 哦
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
cout<<iter->first<<” ”<<iter->second<<end;
int nSize = mapStudent.size();
for(int nIndex = 1; nIndex <= nSize; nIndex++)
cout<<mapStudent[nIndex]<<end;
4、清空
用.clear()函数清空
用.empty()判断是否为空
4、删除
//如果要删除1,用迭代器删除
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);
//如果要删除1,用关键字删除
Int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
//用迭代器,成片的删除
//一下代码把整个map清空
mapStudent.earse(mapStudent.begin(), mapStudent.end());
//成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
PS:%.4f能过 %.4lf不能过╮(╯▽╰)╭
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3fusing namespace std;int cnt;string s;int main(){ cnt=0; map<string,int> ty; while(getline(cin,s)) { ty[s]++; cnt++; } map<string,int>::iterator iter; for(iter=ty.begin();iter!=ty.end();iter++) { cout<<iter->first; printf(" %.4f\n",(iter->second*100.0)/cnt); } return 0;}
- poj 2418 Hardwood Species --- 再来复习遍map吧
- poj 2418 Hardwood Species (map)
- POJ 2418 Hardwood Species(map)
- POJ 2418--Hardwood Species【map】
- POJ 2418 Hardwood Species(map)
- poj 2418 Hardwood Species(使用STL map)
- POJ 2418 Hardwood Species (map练习)
- POJ 2418-Hardwood Species(map)
- POJ 2418 Hardwood Species(map运用)
- POJ 题目2418 Hardwood Species(map)
- POJ 2418 Hardwood Species (字典树||MAP)
- POJ 2418 Hardwood Species (trie树\map)
- POJ 2418 Hardwood Species【字典树 / map】
- poj 2418 Hardwood Species
- POJ 2418 Hardwood Species
- poj 2418Hardwood Species
- POJ 2418 Hardwood Species
- poj 2418 Hardwood Species
- 翻转部分链表
- A Survey of Virtual Machine System: Current Technology and Future Trends
- Matlab备忘录
- Qt学习——与Access数据库建立连接
- how to configure libvirt to use virtio with KVM guests
- poj 2418 Hardwood Species --- 再来复习遍map吧
- NAT原理及穿透技术总结资料
- gcc 优化选项 -O1 -O2 -O3 -Os 优先级
- 强大的矩阵奇异值分解(SVD)及其应用
- 简单搜索(数独)poj 2676 Sudoku
- GitHub操作之删除repo
- 核心电路芯片中作用
- JUCE UI framework
- Python logo面向对象编程语言 Python