zoukankan      html  css  js  c++  java
  • C++ set简介及简单应用

    set中所有元素都会根据元素的键值自动排序,set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值。set不允许两个元素有相同的键值。

    set的各成员函数列表如下:

    1. begin()--返回指向第一个元素的迭代器

    2. clear()--清除所有元素

    3. count()--返回某个值元素的个数

    4. empty()--如果集合为空,返回true

    5. end()--返回指向最后一个元素的迭代器

    6. equal_range()--返回集合中与给定值相等的上下限的两个迭代器

    7. erase()--删除集合中的元素

    8. find()--返回一个指向被查找到元素的迭代器

    9. get_allocator()--返回集合的分配器

    10. insert()--在集合中插入元素

    11. lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器

    12. key_comp()--返回一个用于元素间值比较的函数

    13. max_size()--返回集合能容纳的元素的最大限值

    14. rbegin()--返回指向集合中最后一个元素的反向迭代器

    15. rend()--返回指向集合中第一个元素的反向迭代器

    16. size()--集合中元素的数目

    17. swap()--交换两个集合变量

    18. upper_bound()--返回大于某个值元素的迭代器

    19. value_comp()--返回一个用于比较元素间的值的函数

    举个简单的小栗子,写的麻烦了一点,就是为了顺便练习一下文件。

    /*******************************
    author: yomi
    date: 17.4.21
    ps: 输入的单词全部为以s结尾的单词,输出将自动去掉末尾的s,同时将做一个单词排除集,该集合中的单词将原样输出。
    *******************************/
    #include<iostream>
    #include<set>
    #include<map>
    #include<fstream>
    #include<stdexcept>
    #include<sstream>
    using namespace std;
    
    ifstream & open_file(ifstream & in, const string & file)
    {
        in.close();
        in.clear();
        in.open(file.c_str());
        return in;
    }
    void remove_s(ifstream & remove_file, set<string>&char_set)
    {
        set<string>not_change;
        string not_change_word;
        while(remove_file >> not_change_word){
            not_change.insert(not_change_word);
        }
        string word;
        while(cin >> word){
            if(not_change.count(word)){
                char_set.insert(word);
            }
            else{
                word.assign(word, 0, word.size()-1);
                ///char_set.insert(word.begin(), word.end()-1);
                char_set.insert(word);
    
            }
        }
    }
    int main(int argc, char **argv)
    {
       //  by me
        set<string>char_set;
        ifstream set_it;
        if(!open_file(set_it, "text.txt")){
            throw runtime_error("no not_change file");
        }
        remove_s(set_it, char_set);
    
        set<string>::iterator iter = char_set.begin();
        while(iter!=char_set.end()){
            cout << *iter << endl;
            ++iter;
        }
        ///by book  简短得吓到我了 的确没必要开个set
    //    set<string>exclude;
    //    exclude.insert("success");
    //    exclude.insert("class");
    //    string word;
    //    while(cin >> word){
    //        if(!exclude.count(word)){
    //            word.resize(word.size()-1);
    //        }
    //        cout << word << endl;
    //    }
    
        return 0;
    }
    /* by book
    strings
    string
    class
    class
    success
    success
    kis
    ki
    
    */
    /*
    
    H:Visual C++简单C++>g++ 10-24.cpp -o 10-24.exe
    
    H:Visual C++简单C++>10-24 notchange.txt
    success
    oppas
    strings
    class
    ^Z
    class
    oppa
    string
    success
    
    H:Visual C++简单C++>
    */

     

     

     

  • 相关阅读:
    测测你是男是女
    密集恐惧症候群测试图
    弱智的我
    你还单纯么
    压力测试
    理性人与感性人
    [家里蹲大学数学杂志]第248期东北师范大学2013年数学分析考研试题
    [家里蹲大学数学杂志]第254期第五届[2013年]全国大学生数学竞赛[数学类]试题
    PostgreSQL中,如何查表属于哪个数据库
    对PostgreSQL中tablespace 与 database, table的理解
  • 原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/8253042.html
Copyright © 2011-2022 走看看