C#中泛型容器Stack<T>
我以前都是学出c,c++,这个学期开始学c#有点不适应,在编程中遇到些问题,所以自己在网上查了些资料,翻了一下书,写一些总结。
关于c#中Stack<T>泛型容器:
《1》stack,是一种数据结构——栈,是一种操作受到限制的线性表,只能在一端插入和删除,FILO(first input Last Output)或LIFO(last input first Output)
我们不用去管它在编译器中是采用什么样的存储结构。
《2》泛型容器:泛型类和泛型方法兼复用性、类型安全和高效率于一身,是与之对应的非泛型的类和方法所不及。泛型广泛用于容器(collections)和对容器操作的方法中。
.NET框架2.0的类库提供一个新的命名空间System.Collections.Generic,其中包含了一些新的基于泛型的容器类。要查找新的泛型容器类(collection classes)的示例代码,
请参见基础类库中的泛型。当然,你也可以创建自己的泛型类和方法,以提供你自己的泛化的方案和设计模式,这是类型安全且高效的。下面的示例代码以一个简单的泛型链表
类作为示范。(多数情况下,推荐使用由.NET框架类库提供的List<T>类,而不是创建自己的表。)类型参数T在多处使用,具体类型通常在这些地方来指明表中元素的类型。
《3》Stack<T>在 vs2013中的定义:
namespace System.Collections.Generic
{
// 摘要:
// 表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。
//
// 类型参数:
// T:
// 指定堆栈中的元素的类型。
[Serializable]
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
{
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有默认初始容量。
public Stack();
//
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例包含从指定的集合中复制的元素并且其容量足以容纳所复制的元素数。
//
// 参数:
// collection:
// 从其中复制元素的集合。
//
// 异常:
// System.ArgumentNullException:
// collection 为 null。
public Stack(IEnumerable<T> collection);
//
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。
//
// 参数:
// capacity:
// System.Collections.Generic.Stack<T> 可包含的初始元素数。
//
// 异常:
// System.ArgumentOutOfRangeException:
// capacity 小于零。
public Stack(int capacity); // 摘要:
// 获取 System.Collections.Generic.Stack<T> 中包含的元素数。
//
// 返回结果:
// System.Collections.Generic.Stack<T> 中包含的元素数。
public int Count { get; } // 摘要:
// 从 System.Collections.Generic.Stack<T> 中移除所有对象。
public void Clear();
//
// 摘要:
// 确定某元素是否在 System.Collections.Generic.Stack<T> 中。
//
// 参数:
// item:
// 要在 System.Collections.Generic.Stack<T> 中定位的对象。对于引用类型,该值可以为 null。
//
// 返回结果:
// 如果在 System.Collections.Generic.Stack<T> 中找到 item,则为 true;否则为 false。
public bool Contains(T item);
//
// 摘要:
// 从指定数组索引开始将 System.Collections.Generic.Stack<T> 复制到现有一维 System.Array 中。
//
// 参数:
// array:
// 作为从 System.Collections.Generic.Stack<T> 复制的元素的目标位置的一维 System.Array。System.Array
// 必须具有从零开始的索引。
//
// arrayIndex:
// array 中从零开始的索引,将在此处开始复制。
//
// 异常:
// System.ArgumentNullException:
// array 为 null。
//
// System.ArgumentOutOfRangeException:
// arrayIndex 小于零。
//
// System.ArgumentException:
// 源 System.Collections.Generic.Stack<T> 中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。
public void CopyTo(T[] array, int arrayIndex);
//
// 摘要:
// 返回 System.Collections.Generic.Stack<T> 的一个枚举数。
//
// 返回结果:
// 用于 System.Collections.Generic.Stack<T> 的 System.Collections.Generic.Stack<T>.Enumerator。
public Stack<T>.Enumerator GetEnumerator();
//
// 摘要:
// 返回位于 System.Collections.Generic.Stack<T> 顶部的对象但不将其移除。
//
// 返回结果:
// 位于 System.Collections.Generic.Stack<T> 顶部的对象。
//
// 异常:
// System.InvalidOperationException:
// System.Collections.Generic.Stack<T> 为空。
public T Peek();
//
// 摘要:
// 移除并返回位于 System.Collections.Generic.Stack<T> 顶部的对象。
//
// 返回结果:
// 从 System.Collections.Generic.Stack<T> 的顶部移除的对象。
//
// 异常:
// System.InvalidOperationException:
// System.Collections.Generic.Stack<T> 为空。
public T Pop();
//
// 摘要:
// 将对象插入 System.Collections.Generic.Stack<T> 的顶部。
//
// 参数:
// item:
// 要推入到 System.Collections.Generic.Stack<T> 中的对象。对于引用类型,该值可以为 null。
public void Push(T item);
//
// 摘要:
// 将 System.Collections.Generic.Stack<T> 复制到新数组中。
//
// 返回结果:
// 新数组,包含 System.Collections.Generic.Stack<T> 的元素的副本。
public T[] ToArray();
//
// 摘要:
// 如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.Stack<T> 中的实际元素数。
public void TrimExcess(); // 摘要:
// 枚举 System.Collections.Generic.Stack<T> 的元素。
[Serializable]
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{ // 摘要:
// 获取枚举数当前位置的元素。
//
// 返回结果:
// System.Collections.Generic.Stack<T> 中位于枚举数当前位置的元素。
//
// 异常:
// System.InvalidOperationException:
// 枚举数定位在该集合的第一个元素之前或最后一个元素之后。
public T Current { get; } // 摘要:
// 释放由 System.Collections.Generic.Stack<T>.Enumerator 使用的所有资源。
public void Dispose();
//
// 摘要:
// Advances the enumerator to the next element of the System.Collections.Generic.Stack<T>.
//
// 返回结果:
// 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
//
// 异常:
// System.InvalidOperationException:
// 在创建了枚举数后集合被修改了。
public bool MoveNext();
}
}
}
《4》Stack<T>中方法
名称 | 说明 | |
---|---|---|
![]() |
Clear() |
从 Stack<T> 中移除所有对象。 |
![]() |
Contains(T) |
确定某元素是否在 Stack<T> 中。 |
![]() |
CopyTo(T[], Int32) |
从特定的数组索引处开始,将 Stack<T> 复制到现有一维 Array。 |
![]() |
Equals(Object) |
确定指定的对象是否等于当前对象。(从 Object 继承。) |
![]() |
Finalize() |
在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(从 Object 继承。) |
![]() |
GetEnumerator() |
返回的枚举数 Stack<T>。 |
![]() |
GetHashCode() |
作为默认哈希函数。(从 Object 继承。) |
![]() |
GetType() | |
![]() |
MemberwiseClone() | |
![]() |
Peek() |
返回 Stack<T> 顶部的对象而无需移除它。 |
![]() |
Pop() |
移除并返回位于顶部的对象 Stack<T>。 |
![]() |
Push(T) |
将对象插入 Stack<T> 的顶部。 |
![]() |
ToArray() |
将 Stack<T> 复制到新数组。 |
![]() |
ToString() |
返回表示当前对象的字符串。(从 Object 继承。) |
![]() |
TrimExcess() |
如果元素数小于当前容量的 90%,将容量设置为 Stack<T> 中的实际元素数。 |
注:摘自https://msdn.microsoft.com/zh-cn/library/3278tedw.aspx;
《5》代码示例:
using System;
using System.Collections.Generic; class Example
{
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five"); // A stack can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
} Console.WriteLine("\nPopping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next item to destack: {0}",
numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop()); // Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Stack<string> stack2 = new Stack<string>(numbers.ToArray()); Console.WriteLine("\nContents of the first copy:");
foreach( string number in stack2 )
{
Console.WriteLine(number);
} // Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * ];
numbers.CopyTo(array2, numbers.Count); // Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
Stack<string> stack3 = new Stack<string>(array2); Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in stack3 )
{
Console.WriteLine(number);
} Console.WriteLine("\nstack2.Contains(\"four\") = {0}",
stack2.Contains("four")); Console.WriteLine("\nstack2.Clear()");
stack2.Clear();
Console.WriteLine("\nstack2.Count = {0}", stack2.Count);
}
}
C#中泛型容器Stack<T>的更多相关文章
- C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能
.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...
- (转)Java中的容器详细总结
Java中的容器详细总结(编辑中) 原文链接:http://anxpp.com/index.php/archives/656/ 注:本文基于 Jdk1.8 编写 通常程序总是根据运行时才知道的某些条件 ...
- 泛型容器单元(Generics.Collections)[3]: TStack<T> 堆栈列表
TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性:Push(压栈).Pop(出栈).Peek(查看下一个 ...
- java中的容器问题
小小的总结一下java中的容器问题. 一.三个知识点 1.迭代器 1).java.util.Interator + hasnext(); next(); remove(); 2).java.lang. ...
- C#中泛型和单链表
泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类 ...
- 容器vector的使用总结 容器stack(栈)
0.头文件:#include<vector>; using namespace std; 1.定义: vector<type> vec; 2.迭代器 vector<typ ...
- STL中的容器介绍
STL中的容器主要包括序列容器.关联容器.无序关联容器等. 一]序列容器 (1) vector vector 是数组的一种类表示,提供自动管理内存的功能,除非其他类型容器有更好满足程序的要求,否则,我 ...
- C#中泛型的解释(object,list,var,dynamic的区别)
泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的 ...
- [C++]STL中的容器
C++11 STL中的容器 一.顺序容器: vector:可变大小数组: deque:双端队列: list:双向链表: forward_list:单向链表: array:固定大小数组: string: ...
随机推荐
- 虚拟攻防系统 HoneyPot
转载原地址 http://www.2cto.com/Article/200410/9.html Honeypot 是一个故意设计为有缺陷的系统,通常是用来对入侵者的行为进行警报或者 诱骗.传统的 Ho ...
- opencv 矩阵的相似性对比 (图片之间比较)
测试图片: code: #include <opencv\cv.h> #include <opencv\highgui.h> #include <opencv\c ...
- UVaLive 6859 Points (几何,凸包)
题意:给定 n 个点,让你用最长的周长把它们严格包围起来,边长只能用小格子边长或者是小格子对角线. 析:先把每个点的上下左右都放到一个集合中,然后求出一个凸包,然后先边长转成题目的方式,也好转两个点的 ...
- C#中垃圾回收与内存管理机制
今天抽空来讨论一下.Net的垃圾回收与内存管理机制,也算是完成上个<WCF分布式开发必备知识>系列后的一次休息吧.以前被别人面试的时候问过我GC工作原理的问题,我现在面试新人的时候偶尔也会 ...
- linux的shell脚本入门
Linux shell脚本入门教程 为什么要进行shell编程 在Linux系统中,虽然有各种各样的图形化接口工具,但是sell仍然是一个非常灵活 的工具.Shell不仅仅是命令的收集,而且是一门非常 ...
- hadoop2.1.0编译安装教程
由于现在hadoop2.0还处于beta版本,在apache官方网站上发布的beta版本中只有编译好的32bit可用,如果你直接下载安装在64bit的linux系统的机器上,运行会报一个INFO ut ...
- Javascript 原型继承(续)—从函数到构造器的角色转换
对于每一个声明的函数,里边都会带有一个prototype成员,prototype会指向一个对象,现在我们来聚焦prototype指向的这个对象,首先我们会认为,这个对象是一个该函数对应的一个实例对象, ...
- Gym 100637F F. The Pool for Lucky Ones 暴力
F. The Pool for Lucky Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- MVC风格
MVC风格 点击了解很多其它软件体系结构风格 §模型-视图-控制器风格常被简称为MVC风格 §组件:模型.视图.控制器 §连接件:显式调用.隐式调用.其它机制(比如:Http协议) 工作机制: Mod ...
- 利用text插件和css插件优化web应用
JavaScript的模块化开发到如今,已经相当成熟了,当然,一个应用包含的不仅仅有js,还有html模板和css文件. 那么,如何将html和css也一起打包,来减少没必要的HTTP请求数呢? 本文 ...