Swift, Collection

Source: Internet
Author: User

1. Create a collection (unordered and non-repeatable)

(1) Create an empty collection

var a=set<int> ()//[]

(2) Create a collection

var a:set=[1,2,3]//[2,3,1]

2. Insert in the middle of the collection

var A:set=[1,2,3]a.insert (4)//[2,4,3,1]

3, set (remove) Delete

var a:set=[1,2,3]a.remove (2)//[3,1]

4. Whether the set (contains) has this element

var a:set=[1,2,3]print (A.contains (2))//true

5. Set conversion (sorted) to an array

var a:set=[1,2,3]var b=a.sorted ()//[1,2,3]

6. Special application of Set, subset, parent set, intersection, set, complement, difference set

(1) Intersection

Let X:set=[1,2,3]let y:set=[3,4,5]let z=x.intersection (y)//[3]

(2) Difference set

var x:set=[1,2,3]var y:set=[3,4,5]x.subtract (y)    //modify itself x,[2,1]

(3) and set

var x:set=[1,2,3]var y:set=[3,4,5]var z=x.union (y)//[3,4,1,5,2]

(4) Complementary set

var x:set=[1,2,3]var y:set=[3,4,5]var z=x.symmetricdifference (y)    //[5,2,4,1]

(5) determining whether a subset

var x:set=[3,4,5]var y:set=[3,4,5]var Z=x.issubset (of:y)//x is not a subset of Y, Truevar q=x.isstrictsubset (of:y)//strict subset, False

(6) Determine if the parent set

var x:set=[3,4,5]var y:set=[3,4,5]var Z=x.issuperset (of:y)//x is not a parent set of Y, Truevar q=x.isstrictsuperset (of:y)//strict parent set, False

(7) Determine if there is an intersection

var x:set=[3]var y:set=[3,4,5]var z=x.isdisjoint (with:y)//false

  

  

  

Swift, Collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.