zoukankan      html  css  js  c++  java
  • STL之set和multiset(集合)

    set和multiset会根据特定的排序准则,自动将元素进行排序。不同的是后者允许元素重复而前者不允许。 constructing sets
    #include
    #include
    using namespace std ;
    int main()  {
        int a[] = {2,1,3,4,5,6,8,7} ;
        set s(a,a+8) ;
        set::iterator iter = s.begin() ;
        for( ; iter != s.end() ; iter++)
            cout << *iter << " " ;
        cout << endl ;
        return 0 ;
    }
    
    向对象中插入元素:
    #include
    #include
    using namespace std ;
    int main()  {
        set s;
        int a[] = {2,1,3,4,5,6,8,7} ;
        for(int i = 0 ; i < 8 ; i++)
            s.insert(a[i]) ;
        set::iterator iter = s.begin() ;
        for( ; iter != s.end() ; iter++)
            cout << *iter << " " ;
        cout << endl ;
        return 0 ;
    }
    
    删除对象中的元素:
    #include
    #include
    using namespace std ;
    int main()  {
        int a[] = {2,1,3,4,5,6,8,7} ;
        set s(a,a+8);
    
        s.erase(s.begin()) ;
        set::iterator iter = s.begin() ;
        for( ; iter != s.end() ; iter++)
            cout << *iter << " " ;
        cout << endl ;
    
        s.erase(s.find(8)) ;
        set::iterator iter1 = s.begin() ;
        for( ; iter1 != s.end() ; iter1++)
            cout << *iter1 << " " ;
        cout << endl ;
    
        s.erase(s.begin(),s.end()) ;
        set::iterator iter2 = s.begin() ;
        for( ; iter2 != s.end() ; iter2++)
            cout << *iter2 << " " ;
        cout << endl ;
        return 0 ;
    }
    
    count计数函数:
    #include
    #include
    using namespace std ;
    int main()  {
        int a[] = {2,1,3,4,5,6,8,7,8,8} ;   //multiet允许元素重复
        multiset s(a,a+10);
        cout << s.count(8) << endl ;
        return 0 ;
    }
    
  • 相关阅读:
    Java for LeetCode 052 N-Queens II
    Java for LeetCode 051 N-Queens
    Java for LeetCode 050 Pow(x, n)
    Java for LeetCode 049 Anagrams
    15-[JavaScript]-ECMAScript 1
    4-观看答辩有感
    51-小米官网
    14-HTML-CSS案例
    13-[CSS]-postion位置:相relative,绝absolute,固fixed,static(默认),z-index
    12-[CSS]-margin塌陷,margin 0 auto,善用父级的padding
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4236861.html
Copyright © 2011-2022 走看看