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

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

二、set对象插入元素
插入方法
说明
set set1;
set1.insert("the");
set1.insert("and");
如果键不在set对象中,则插入一个该键的新元素;如果该键在 set对象中已存在,则保持set 对象不变。 该函数返回一个pair类型的对象,包含一个指向该键元素的set迭代器,以及一个bool类型的对象,表示是否插入了该元素。
set 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 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

// using set to store removed word
void restricted_wc_v1(ifstream &remove_file, map &word_count)
{
    set 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 &word_count)
{
    vector 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::iterator iter = excluded.begin();
        while (iter != excluded.end())
        {
            if (word == *iter)
            {
                find = true;
                break;
            }
            iter++;
        }
       
        if (!find)
        {
            ++word_count[word];
        }
    }

    return;
}

int main()
{
    map 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::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 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    set 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    vector books;
    set 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::iterator sit = readedBooks.begin(); sit != readedBooks.end(); ++sit)
        {
            cout << *sit << endl;
        }

        cout << "Book not read: " << endl;
        for (vector::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


你可能感兴趣的:(C++)