【C++ Primer】【学习笔记】【第十章】关联容器之:set类型

一、set容器定义
set容器支持大部分的map操作,但如下两种操作除外:
1、set不支持下标操作;
2、set没有定义mapped_type类型。
注:set存储的元素仅仅是键,而不存储所关联的值。与map一样,set容器存储的键也必须唯一,而且不能修改。

二、set对象插入元素
插入方法
说明
set<string> set1;
set1.insert("the");
set1.insert("and");
如果键不在set对象中,则插入一个该键的新元素;如果该键在 set对象中已存在,则保持set 对象不变。 该函数返回一个pair类型的对象,包含一个指向该键元素的set迭代器,以及一个bool类型的对象,表示是否插入了该元素。
set<int> iset2;
iset2.insert(ivec.begin(), ivec.end());
ivec.begin()ivec.end()是标记元素范围的迭代器。对于这些元素,如果其键在set对象中不存在,则将该键对应的元素插入m。 返回值为void。

三、set对象获取元素
查询方法
说明
iset2.count(k)
返回set容器中k的出现次数。返回值只能是0或者1,因为set容器 存储的键必须唯一
iset2.find(k)
如果set容器中存在按k索引的元素,则返回指向该元素的迭代器。如果不存在,则返回超出末端迭代器。
注:使用返回的迭代器,只能对值进行读取,而不能进行修改。因为set容器中的键是const类型的。


习题10.23:单词统计,排除黑名单中的词

#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <set>
using namespace std;

// using set to store removed word
void restricted_wc_v1(ifstream &remove_file, map<string, int> &word_count)
{
    set<string> excluded;
    string remove_word;
    while (remove_file >> remove_word)
    {
        excluded.insert(remove_word);
    }

    cout << "Enter text1(Ctrl + D or Ctrl + Z to end): " << endl;
    string word;
    while (cin >> word)
    {
        if (!excluded.count(word))
        {
            ++word_count[word];
        }
    }

    return;
}

// using vector to store removed word
void restricted_wc_v2(ifstream &remove_file, map<string, int> &word_count)
{
    vector<string> excluded;
    string remove_word;
    while (remove_file >> remove_word)
    {
        excluded.push_back(remove_word);
    }

    cout << "Enter text2(Ctrl + D or Ctrl + Z to end): " << endl;
    string word;
    while (cin >> word)
    {
        bool find = false;

        vector<string>::iterator iter = excluded.begin();
        while (iter != excluded.end())
        {
            if (word == *iter)
            {
                find = true;
                break;
            }
            iter++;
        }
       
        if (!find)
        {
            ++word_count[word];
        }
    }

    return;
}

int main()
{
    map<string, int> word_count;
    string filename;

    cout << "Enter filename: " << endl;
    cin >> filename;
    ifstream filestream(filename.c_str());
    if (!filestream)
    {
        cout << "Error: open file fail!" << endl;
        return -1;
    }

    restricted_wc_v2(filestream, word_count);

    cout << "word\t\t" << "times" << endl;
    map<string, int>::iterator iter = word_count.begin();
    while (iter != word_count.end())
    {
        cout << iter->first << "\t\t" << iter->second << endl;
        iter++;
    }

    return 0;
}
[chapter10]$ ./a.out
Enter filename:
name
Enter text2(Ctrl + D or Ctrl + Z to end):
yj zs ls ww zl
word            times
yj              1
zl              1


习题10.24:单词从复数转换成单数

#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <set>
using namespace std;

int main()
{
    set<string> excluded;

    // setup excluded
    excluded.insert("class");
    excluded.insert("success");
    // new word add here...

    string word;
    cout << "Enter a word(Ctrl + D or Ctrl + Z): " << endl;
    while (cin >> word)
    {
        if (!excluded.count(word))
        {
            word.resize(word.size() - 1);
        }

        cout << "non-plural version: " << word << endl;
        cout << "Enter a word(Ctrl + D or Ctrl + Z): " << endl;
    }

    return 0;
}
[chapter10]$ ./a.out 
Enter a word(Ctrl + D or Ctrl + Z): 
class
non-plural version: class
Enter a word(Ctrl + D or Ctrl + Z): 
success
non-plural version: success
Enter a word(Ctrl + D or Ctrl + Z): 
bikes
non-plural version: bike
Enter a word(Ctrl + D or Ctrl + Z): 
tables    
non-plural version: table
Enter a word(Ctrl + D or Ctrl + Z): 


习题10.25:读书列表管理

#include <iostream>
#include <fstream>
#include <string>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <ctime>
using namespace std;

int main()
{
    vector<string> books;
    set<string> readedBooks;
    string name;

    // establish books to read
    cout << "Enter names for books you'd like to read.(Ctrl + D or Ctrl + Z to end): " << endl;
    while (cin >> name)
    {
        books.push_back(name);
    }
    cin.clear();

    bool timeOver = false;
    string answer, bookName;
    srand((unsigned)time(NULL));

    while (!timeOver & !books.empty())
    {
        cout << "Would you like to read a book?(Yes/No): " << endl;
        cin >> answer;

        if ('y' == answer[0] || 'Y' == answer[0])
        {
            int i = rand() % books.size();
            bookName = books[i];
            cout << "You can read this book: " << bookName << endl;
            readedBooks.insert(bookName);
            books.erase(books.begin() + i);
            
            cout << "Did you read it?(Yes/No): " << endl;
            cin >> answer;
            if ('n' == answer[0] || 'N' == answer[0])
            {
                readedBooks.erase(bookName);
                books.push_back(bookName);
            }
        }

        cout << "Time over?(Yes/No): " << endl;
        cin >> answer;
        if ('y' == answer[0] || 'Y' == answer[0])
        {
            timeOver = true;
        }
    }

    if (timeOver)
    {
        cout << "Books read: " << endl;
        for (set<string>::iterator sit = readedBooks.begin(); sit != readedBooks.end(); ++sit)
        {
            cout << *sit << endl;
        }

        cout << "Book not read: " << endl;
        for (vector<string>::iterator vit = books.begin(); vit != books.end(); ++vit)
        {
            cout << *vit << endl;
        }
    }
    else
    {
        cout << "Congratulatios! You've read all these books!" << endl;
    }

    return 0;
}
[chapter10]$ ./a.out 
Enter names for books you'd like to read.(Ctrl + D or Ctrl + Z to end): 
abc def ghi jkl
Would you like to read a book?(Yes/No): 
y
You can read this book: def
Did you read it?(Yes/No): 
y
Time over?(Yes/No): 
n
Would you like to read a book?(Yes/No): 
y
You can read this book: jkl
Did you read it?(Yes/No): 
y
Time over?(Yes/No): 
y
Books read: 
def
jkl
Book not read: 
abc
ghi


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值