第十一章鱼哥

#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这个方法是不必的。

vb.net自定义控件可以通过继承现有的控件类来创建新的控件。章鱼哥是一种自定义控件,通过继承基类控件,可以为章鱼哥控件添加特定的功能和样式。 首先,在Visual Studio中创建一个新的Windows Forms项目。然后,在解决方案资源管理器中右键单击项目名称,选择"添加",再选择"用户控件",命名为"章鱼哥"。 接下来,双击打开"章鱼哥.vb"代码文件,在代码中定义章鱼哥控件的属性和方法。可以根据需要为章鱼哥控件添加各种恶搞、逗趣和有趣的特性,比如章鱼哥的眼睛会跟随鼠标移动,章鱼哥的触手可以拖动等等。 在代码中添加绘制控件的方法,可以使用绘图对象绘制章鱼哥的外观,还可以使用控件的事件来响应用户的交互操作。 完成控件的设计后,可以将章鱼哥控件添加到窗体上进行使用。在主窗体的设计器中,可以找到工具箱中的章鱼哥控件并将其拖放到窗体上,然后可以调整控件的位置和大小。 在代码中,可以通过访问章鱼哥控件的属性和方法来设置其特定的行为和样式。可以监控章鱼哥控件的事件,比如鼠标点击、拖动或键盘按下等,以便实现特定的功能。 最后,可以对项目进行编译和运行,就可以看到窗体上显示着一个可爱的章鱼哥控件了。通过自定义控件,可以为应用程序添加个性化和有趣的功能,提升用户体验和吸引力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值