zoukankan      html  css  js  c++  java
  • C#编程(五十五)----------HashSet和SortedSet

    饱含不重复元素的集合称为”集(set)”. .NET4包含两个集(HashSet<T>SortedSet<T>),他们都实现ISet<T>接口.HashSet<T>即包含不重复元素的无序列表,SortedSet<T>集包含不重复元素的有序列表.

    ISet<T>接口提供的方法可以创建合集,交集,或者给出一个集合时另一个集的超集或子集的信息.

    案例:

                //使用HashSet:重复的元素自动被移除,但是不排序

                var set = new HashSet<int>() { 5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9 };

                foreach (var item in set)

                {

                    Console.WriteLine(item);

                }

                Console.ReadKey();

    同样的代码,HashSet换成SortedSet:

                //使用SortedSet:重复的元素自动被移除,还进行了排序

                var set = new SortedSet<int>() { 5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9 };

                foreach (var item in set)

                {

                    Console.WriteLine(item);

                }

                Console.ReadKey();

    总结:

    1.HashSetSortedSet主要的作用是用来进行两个集合求交集,并集,差集等运算. 集合中包含一组不重复出现且无特性顺序的元素.前者不会自动排序,后者会加入元素后,自动排序

    2.两者都无法从特定位置访问其中某个元素.

    3.可以使用其查找功能:

    Set.Contains(“value”) //返回truefalse

    4.对集合的操作:

    a . SymmetricExceptWith:仅包含该对象或指定集合中存在的元素(但不可同时包含两者中的元素).去除交集,剩下两个集合元素.

    b . UnionWith:包含该对象本身和制定集合中存在的所有元素. 并集

    c . ExceptWith从当前HashSet<T>对象中移除指定集合中的所有元素 . 差集

    d . IntersectWith:仅包含该对象和指定集合中存在的元素. 交集

    5.SortedSet对象,可以调用GetViewBetween,Max,Min方法

    6.除了SortedSet,System.Collections.Generic命名空间下,还提供了SortedDictionarySortedList两个类.

    测试HashSet内置的一些方法:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace

    {

        class Program

        {

            static void Main(string[] args)

            {

                HashSet<char> setA = new HashSet<char>();

                HashSet<char> setB = new HashSet<char>();

                setA.Add('A');

                setA.Add('B');

                setA.Add('C');

                setB.Add('C');

                setB.Add('D');

                setB.Add('E');

                Show("Initial content of setA: ", setA);

                Show("Initial content of setB: ", setB);

                setA.SymmetricExceptWith(setB);   //setAsetB 各自特有、对方没有的元素列出来

                Show("setA after Symmetric difference with SetB: ", setA);

                setA.UnionWith(setB);       //setAsetB 的全部元素列出来 (union 并集)

                Show("setA after union with setB: ", setA);

                setA.ExceptWith(setB);      //setA 中,所拥有的 setB 元素移除

                Show("setA after subtracting setB: ", setA);

                Console.WriteLine();

                Console.Read();

            }

            static void Show(string msg, HashSet<char> set)

            {

                Console.Write(msg);

                foreach (char ch in set)

                    Console.Write(ch + " ");

                Console.WriteLine();

            }

        }

    }

    测试SortedSet的方法:

    using System;

    using System.Collections.Generic;

    using System.Linq;//此为Max(),Min()方法的必要调用

    using System.Text;

    using System.Threading.Tasks;

    namespace

    {

        class Program

        {

            static void Main(string[] args)

            {

                var set = new SortedSet<int>() { 5, 9, 2, 1, 2, 2, 3, 7, 4, 9, 9 };

                foreach (int element in set)

                    Console.WriteLine(string.Format(" {0}", element));

               

                Console.WriteLine("Max: " + set.Max() );

                Console.WriteLine("Min: " + set.Min() );

                Console.Write("<br>2 ~ 5 之间的值: ");

                //只取值为 2 ~ 5 之间的元素

                var subSet = set.GetViewBetween(2, 5);

                foreach (int i in subSet)

                {

                    Console.Write(i + " ");

                }

                Console.WriteLine();

                Console.Read();

            }

            static void Show(string msg, HashSet<char> set)

            {

                Console.Write(msg);

                foreach (char ch in set)

                    Console.Write(ch + " ");

                Console.WriteLine();

            }

        }

    }

  • 相关阅读:
    编译 | 更新标准库_交叉编译工具链
    论文 | 图文_学科
    编码 | 二进制格式设计方案
    图片 | 图片上传管理
    进程 | 查询进程中包含多少线程
    第二周02:Fusion ICP逐帧融合
    exe文件当前目录搜索文件
    第一周:读取XML深度数据并将其重建为三维点云
    第二周:01 ICP迭代交互
    C++文件读写(转载)
  • 原文地址:https://www.cnblogs.com/FinleyJiang/p/7602715.html
Copyright © 2011-2022 走看看