STL单词统计C++(map容器的使用)

题目描述
输入n组单词,统计不同单词出现的个数,并按照字典顺序输出。(n<1000,每个单词长度小于1000)
样例输入
4
aab
aab
aa
bc
样例输出
aa 1
aab 2
bc 1

使用map容器可以比较容易做出

#include <iostream>
#include <map>
#include <iterator>
#include <stdlib.h>
using namespace std;
 
main()
{
     typedef map<string,long >word;
     int len ;
     word pairs;  
     long k = 1;
     cin>>len;
     string *f = new string [len];
     for(int i=0;i<len;i++)
     {
         cin>>f[i];
         if(pairs.count(f[i]) == 0)
         {
            pairs.insert(make_pair(f[i],k));
         }
         else{
            pairs[f[i]] ++;
         }
       
  }
     word::iterator j;
     for(j = pairs.begin(); j!= pairs.end(); j++)
     cout<<j->first<<" "<<j->second <<endl;
     pairs.clear();
     return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的map是一个关联容器,它提供了一种键值对的映射。它使用红黑树实现,插入和查找的时间复杂度都为O(log n)。map的主要应用在以下三个方面: 1. 数据存储与查找 map可以将一个键值对存储在容器中,并可以通过键来查找对应的值。例如,下面的代码演示了如何使用map来存储学生的考试成绩,并通过学生的姓名来查找对应的成绩: ```c++ #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores["Alice"] = 95; scores["Bob"] = 85; scores["Cathy"] = 90; std::cout << "Alice's score is " << scores["Alice"] << std::endl; std::cout << "Bob's score is " << scores["Bob"] << std::endl; std::cout << "Cathy's score is " << scores["Cathy"] << std::endl; return 0; } ``` 输出: ``` Alice's score is 95 Bob's score is 85 Cathy's score is 90 ``` 2. 统计词频 map可以用于统计文本中每个单词出现的次数。例如,下面的代码演示了如何使用map统计字符串中每个单词出现的次数: ```c++ #include <iostream> #include <map> #include <sstream> int main() { std::string text = "the quick brown fox jumps over the lazy dog"; std::map<std::string, int> word_count; std::istringstream iss(text); std::string word; while (iss >> word) { word_count[word]++; } for (auto& wc : word_count) { std::cout << wc.first << " occurs " << wc.second << " times" << std::endl; } return 0; } ``` 输出: ``` brown occurs 1 times dog occurs 1 times fox occurs 1 times jumps occurs 1 times lazy occurs 1 times over occurs 1 times quick occurs 1 times the occurs 2 times ``` 3. 缓存 map可以用于实现缓存,例如,下面的代码演示了如何使用map来实现一个最近最少使用(LRU)缓存: ```c++ #include <iostream> #include <map> #include <list> template<typename K, typename V> class LRUCache { public: LRUCache(int capacity) : capacity_(capacity) {} V get(const K& key) { if (cache_.count(key)) { auto it = cache_[key]; lru_.splice(lru_.begin(), lru_, it); return it->second; } else { return V(); } } void put(const K& key, const V& value) { if (cache_.count(key)) { auto it = cache_[key]; it->second = value; lru_.splice(lru_.begin(), lru_, it); } else { if (cache_.size() == capacity_) { auto& lru_key = lru_.back().first; cache_.erase(lru_key); lru_.pop_back(); } lru_.emplace_front(key, value); cache_[key] = lru_.begin(); } } private: int capacity_; std::map<K, typename std::list<std::pair<K, V>>::iterator> cache_; std::list<std::pair<K, V>> lru_; }; int main() { LRUCache<int, std::string> cache(2); cache.put(1, "foo"); cache.put(2, "bar"); std::cout << cache.get(1) << std::endl; // foo cache.put(3, "baz"); std::cout << cache.get(2) << std::endl; // "" std::cout << cache.get(3) << std::endl; // baz return 0; } ``` 输出: ``` foo baz ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值