System::Collections::Generic::Stack 类
内容
[
隐藏
]Stack class
Stack class forward declaration.
template<typename T>class Stack : public System::Collections::Generic::IEnumerable<T>
| 参数 | 描述 |
|---|---|
| T | 元素类型。 |
Nested classes
- Class Enumerator
方法
| 方法 | 描述 |
|---|---|
| AddRange(IEnumerablePtr) | 将元素放入栈中。 |
| virtual Clear() | 删除栈中的所有元素。 |
| virtual Contains(const T&) const | 检查容器中是否存在特定项;使用运算符 == 进行比较。 |
| data() | 内部数据结构访问器。 |
| data() const | 内部数据结构访问器。 |
| virtual get_Count() const | 获取栈中元素的数量。 |
| GetEnumerator() override | 获取枚举器以遍历当前栈。 |
| Peek() | 获取栈顶元素,但保持在栈中。 |
| Pop() | 获取栈顶元素。 |
| Push(const T&) | 放置栈顶元素。 |
| Stack() | 构造空栈。 |
| Stack(int) | 构造空栈。 |
| Stack(IEnumerablePtr) | 拷贝构造函数。 |
| virtual ToArray() | 将栈转换为数组。 |
| virtualizeBeginConstIterator() const override | 获取当前容器的 begin const 迭代器的实现。 |
| virtualizeBeginIterator() override | 获取当前容器的 begin 迭代器的实现。 |
| virtualizeEndConstIterator() const override | 获取当前容器的 end const 迭代器的实现。 |
| virtualizeEndIterator() override | 获取当前容器的 end 迭代器的实现。 |
Typedefs
| 类型定义 | 描述 |
|---|---|
| IEnumerablePtr | 包含相同类型元素的集合。 |
| IEnumeratorPtr | Enumerator 类型。 |
| stack_t | RTTI 信息。 |
| ValueType | 值类型。 |
备注
Stack class wrapping std::list. Objects of this class should only be allocated using System::MakeObject() function. Never create instance of this type on stack or using operator new, as it will result in runtime errors and/or assertion faults. Always wrap this class into System::SmartPtr pointer and use this pointer to pass it to functions as argument.
#include <system/collections/stack.h>
#include <system/smart_ptr.h>
using namespace System;
using namespace System::Collections::Generic;
void PrintItems(const SmartPtr<IEnumerable<int>> &stack)
{
for (const auto item: stack)
{
std::cout << item << ' ';
}
std::cout << std::endl;
}
int main()
{
// 创建 Stack 类实例。
auto stack = MakeObject<Stack<int>>();
// 填充栈。
stack->Push(1);
stack->Push(2);
stack->Push(3);
// 打印栈的最后一个项目。Peek 方法不会从栈中移除项目。
std::cout << stack->Peek() << std::endl;
PrintItems(stack);
// 打印栈的最后一个项目。Pop 方法会从栈中移除项目。
std::cout << stack->Pop() << std::endl;
PrintItems(stack);
return 0;
}
/*
This code example produces the following output:
3
3 2 1
3
2 1
*/
另见
- Class IEnumerable
- Namespace System::Collections::Generic
- Library Aspose.Font for C++