第十一章鱼哥

#include <map>
using std::map;

#include <string>
using std::string;

#include <utility>
using std::pair;

#include <cstddef>
using std::size_t;

#include <iostream>
using std::cin; using std::cout; using std::endl;

int main()
{
    // count the number of times each word occurs in the input
    map<string, size_t> word_count; // empty map from string to size_t
    string word;
    while (cin >> word)
        ++word_count[word];

    for (const auto &w : word_count)
        cout << w.first << " occurs " << w.second << " times" << endl;

    // get an iterator positioned on the first element
    auto map_it = word_count.cbegin();
    // compare the current iterator to the off-the-end iterator
    while (map_it != word_count.cend()) {
        // dereference the iterator to print the element key--value pairs
        cout << map_it->first << " occurs "
            << map_it->second << " times" << endl;
        ++map_it;  // increment the iterator to denote the next element
    }

    return 0;
}

map和set采用默认初始化
对于有序容器,必须定义比较的方法。默认情况下,标准库使用<来比较两个关键字(<要满足严格弱序所有条件)

定义一个Mutiset

mutiset

string search_item("Alain de Botton");
auto entries=authors.count(search_item);
auto iter=authors.find(search_item);
while(entries)
{
    cout<<iter->second<<endl;
    ++iter;
    --entries;
}
//当然lower_bound与upper_bound方法同样也可以
//如果元素不存在与容器之中,则lower_bound与Upper_bound返回同样的值。
for(auto beg=authors.lower_bound(search_item),end=authors.upper_bound(search_item);beg!=end;++beg)
    cout<<beg->second<<endl;
//此外,还有equal_range,该方法返回一个pair,第一个迭代器返回首个元素,第二个迭代器指向最后一个元素之后的位置。

for(auto pos=authors.equal_range(search_item);
    pos.first!=pos.second;++pos.first)
    cout<<pos.first->second<<endl;

无序容器实际上使用的是哈希相关方法。或者更确切的说,是链地址法。
因而,使用.begin,.end函数时要加入参数int,指明是第几个链条上的迭代器。

#include "Version_test.h"

#include <unordered_map>
using std::unordered_map;

#include <unordered_set>
using std::unordered_set; using std::unordered_multiset;

#include <string>
using std::string; 

using std::hash;

#include <iostream>
using std::cin; using std::cout; using std::endl;

#include <cstddef>
using std::size_t;

#include "Sales_data.h"

// unordered_map version of the word count program
int main() 
{
    // count occurrences, but the words won't be in alphabetical order
    unordered_map<string, size_t> word_count;  
    string word;
    while (cin >> word)
        ++word_count[word]; // fetch and increment the counter for word

    for (const auto &w : word_count) // for each element in the map
        // print the results
        cout <<  w.first << " occurs " << w.second 
             << ((w.second > 1) ? " times" : " time") << endl;

    return 0;
}

// how to override default hash and equality operator on key_type
size_t hasher(const Sales_data &sd) 
{
    return hash<string>()(sd.isbn());
}
bool eqOp(const Sales_data &lhs, const Sales_data &rhs)
{
    return lhs.isbn() == rhs.isbn();
}

// type alias using our functions in place of hash<key_type> and ==
#ifdef TYPE_ALIAS_DECLS
using SD_multiset = unordered_multiset<Sales_data, 
                    decltype(hasher)*, decltype(eqOp)*>;
#else
typedef
unordered_multiset<Sales_data, decltype(hasher)*, decltype(eqOp)*> SD_multiset;
#endif

// bookstore can hold multiple Sales_data with the same ISBN
// arguments are the bucket size 
// and pointers to the hash function and equality operator
SD_multiset bookstore(42, hasher, eqOp);

// how to override just the hash function;
// Foo must have ==
struct Foo { string s; };

// we'll see how to define our own operators in chapter 14
bool operator==(const Foo& l, const Foo&r) { return l.s == r.s; }

size_t FooHash(const Foo& f) { return hash<string>()(f.s); }

// use FooHash to generate the hash code; Foo must have an == operator
unordered_set<Foo, decltype(FooHash)*> fooSet(10, FooHash);

当然,如果类定义了==操作,那么eqop这个方法是不必的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
项目:用 JavaScript 编写的《小鸡快跑》游戏 小鸡快跑游戏是一个 javascript 项目。这是一款简单而有趣的游戏,你可能将它列入你的愿望清单中。这款游戏主要包含 javascript 和一些 CSS,以使其看起来很漂亮。这款游戏与著名的谷歌浏览器离线游戏恐龙跑者非常相似。 游戏玩法 游戏玩法简单有趣。你所要做的就是控制你的小鸡,跳过障碍物。这只鸡的名字叫“Pepper”。在这里,你必须让 Pepper 跳起来,并尽量避开即将到来的威胁。控制键是 W、空格键或向上箭头键。 当你继续游戏时,游戏速度通常会以更快的速度增加。试着按下控制按钮跳得更高,这样 Pepper 就可以跳得更高,以避开障碍物。当你最终撞到障碍物时,你终于可以查看你的得分了。此外,你还可以在社交媒体上分享你的得分。 总的来说,这款游戏玩起来相当有趣。此外,作为一名 程序员,它能让你以某种方式更轻松、更完美地提高你的程序编写能力。这款游戏纯粹是用 javascript 制作的。游戏开发过程中没有使用任何类型的框架。 您可以通过查看下面的图片滑块来查看我们的项目 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值