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.

 

posted @ 2015-04-07 01:54  Milkor  阅读(609)  评论(0编辑  收藏  举报