• C++中Set的使用


    /*
    #include <string>    //  使用 string 类时须包含这个文件
    #include <iostream>  //  这个就加上去吧。c++的输入和输出。
    using namespace std; //     这个不能忘记了
    String 类的输入和输出要用cin和cout,否则就爆栈错了
    */
    
    #include <iostream>
    #include <set>
    #include <string>
    using namespace std;
    
    set<string> ::iterator it;
    void print(set<string> iset)
    {
        for (it = iset.begin();it != iset.end();it++)
        {
            cout << *it;
        }
    }
    //函数还是能包装的。set也可以当做参数传入
    int main()
    {
        set<string> iset;
        iset.insert("1");
        iset.insert("2");
        iset.insert("3");
        /* 两种遍历
        set<string> ::iterator it;
        for (it = iset.begin();it != iset.end();it++)
        {
            cout << *it;
        }
    
        set<string> ::reverse_iterator rit;
        for (rit = iset.rbegin();rit != iset.rend();rit++)
        {
            cout << *rit;
        }
        迭代器和函数一一对应的。
        iterator -> begin end
        reverse_iterator -> rbegin rend
        */
    
        /*貌似只能删除键值。
        iset.erase("2");
        print(iset);
        */
    
        /* 返回的是迭代器。
        it = iset.find("1");
        cout << *it;
        */
        
    }
    my own code

    It's beyond my imagination,the Iterator appears in the C++!

    In my view,the Iterator only appears int the Java.(I'm so  shortsighted)

    there isn't any doubt that the Iterator is a powerful tool to traverse  Collection.

     just as you see,you can easily use a simple circle to implement traversal.

    There is another point worth mentioning,the function "find" return Iterator.

    according to my experience.the Iterator is actually a point.

    you can try to output the value of Iterator.Which is always obeyond the range of  Integer.

    It appears more evident that using the '*' to get the actual value of the Iterator.

  • 相关阅读:
    ssh 远程命令
    POJ 2287
    POJ 2376
    hihoCoder1488
    POJ1854
    HDU 5510
    HDU 4352
    CodeForces 55D
    HDU 1517
    CodeForces 1200F
  • 原文地址:https://www.cnblogs.com/Milkor/p/4397300.html
Copyright © 2020-2023  润新知