STL练习程序(set...)

#include 
#include <string>
#include <set>
using namespace std;

int main()
{
    string name[]={"tian","dsp","sp","ti"};
    set<string> setname;

    for (int i=0;i<4;i++)
        setname.insert(name[i]);

    set<string>::iterator it;
    for (it=setname.begin();it!=setname.end();it++)
        cout<<*it<<"  ";

    it=setname.find("dsp");
    cout<<endl;
    cout<<*it<<endl;

    setname.erase(it);
    for (it=setname.begin();it!=setname.end();it++)
        cout<<*it<<"  ";

    cout<endl;

    for (int i=0;i<4;i++)
        if (setname.count(name[i])>0)
            cout<" in set"<<endl;
        else
            cout<" not in set"<<endl;

    setname.clear();
    if (setname.empty())
        cout<<"empty"<<endl;

    system("pause");
    return 0;
}

转载于:https://www.cnblogs.com/tiandsp/archive/2012/07/26/2610272.html

你可能感兴趣的:(STL练习程序(set...))