Skip to content

Commit 5010056

Browse files
committed
Add xtd::collections::generic::stack example
1 parent ca99653 commit 5010056

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

examples/xtd.core.examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@
231231
* [generic_list_reverse](generic_Collections/generic_list_reverse/README.md) shows how to use [xtd::collections::generic::list::reverse](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1list.html) method.
232232
* [generic_ordered_dictionary](generic_Collections/generic_ordered_dictionary/README.md) shows how to use [xtd::collections::generic::ordered_dictionary](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1ordered__dictionary.html) clss.
233233
* [generic_queue](generic_Collections/generic_queue/README.md) shows how to use [xtd::collections::generic::queue](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1queue.html) clss.
234+
* [generic_stack](generic_Collections/generic_stack/README.md) shows how to use [xtd::collections::generic::stack](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1stack.html) clss.
234235
* [mix_generic_list_and_std_vector_api](generic_Collections/mix_generic_list_and_std_vector_api/README.md) shows how to use [xtd::collections::generic::list](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1list.html) class.
235236

236237
## [globalization](globalization/README.md)

examples/xtd.core.examples/generic_collections/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ add_projects(
3030
generic_list_reverse
3131
generic_ordered_dictionary
3232
generic_queue
33+
generic_stack
3334
mix_generic_list_and_std_vector_api
3435
)

examples/xtd.core.examples/generic_collections/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [generic_list_reverse](generic_list_reverse/README.md) shows how to use [xtd::collections::generic::list::reverse](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1list.html) method.
2828
* [generic_ordered_dictionary](generic_ordered_dictionary/README.md) shows how to use [xtd::collections::generic::ordered_dictionary](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1ordered__dictionary.html) clss.
2929
* [generic_queue](generic_queue/README.md) shows how to use [xtd::collections::generic::queue](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1queue.html) clss.
30+
* [generic_stack](generic_stack/README.md) shows how to use [xtd::collections::generic::stack](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1stack.html) clss.
3031
* [mix_generic_list_and_std_vector_api](mix_generic_list_and_std_vector_api/README.md) shows how to use [xtd::collections::generic::list](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1list.html) class.
3132

3233
## Build and run any project
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(generic_stack)
4+
find_package(xtd REQUIRED)
5+
add_sources(README.md src/generic_stack.cpp)
6+
target_type(CONSOLE_APPLICATION)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# generic_stack
2+
3+
Shows how to use [xtd::collections::generic::stack](https://gammasoft71.github.io/xtd/reference_guides/latest/classxtd_1_1collections_1_1generic_1_1stack.html) class.
4+
5+
## Sources
6+
7+
* [src/generic_stack.cpp](src/generic_stack.cpp)
8+
* [CMakeLists.txt](CMakeLists.txt)
9+
10+
## Build and run
11+
12+
Open "Command Prompt" or "Terminal". Navigate to the folder that contains the project and type the following:
13+
14+
```cmake
15+
xtdc run
16+
```
17+
18+
## Output
19+
20+
```
21+
five
22+
four
23+
three
24+
two
25+
one
26+
27+
Popping 'five'
28+
Peek at next item to destack: four
29+
Popping 'four'
30+
31+
Contents of the first copy:
32+
one
33+
two
34+
three
35+
36+
Contents of the second copy, with duplicates and nulls:
37+
one
38+
two
39+
three
40+
41+
42+
43+
44+
stack2.contains("four") = false
45+
46+
stack2.clear()
47+
48+
stack2.count() = 0
49+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <xtd/xtd>
2+
3+
namespace examples {
4+
class program {
5+
public:
6+
static auto main() -> void {
7+
auto numbers = stack<string> {};
8+
numbers.push("one");
9+
numbers.push("two");
10+
numbers.push("three");
11+
numbers.push("four");
12+
numbers.push("five");
13+
14+
// A stack can be enumerated without disturbing its contents.
15+
for (auto number : numbers)
16+
console::write_line(number);
17+
18+
console::write_line("\nPopping '{}'", numbers.pop());
19+
console::write_line("Peek at next item to destack: {}", numbers.peek());
20+
console::write_line("Popping '{}'", numbers.pop());
21+
22+
// Create a copy of the stack, using the to_array method and the constructor that accepts an ienumerable<type_t>.
23+
auto stack2 = stack<string>(numbers.to_array());
24+
25+
console::write_line("\nContents of the first copy:");
26+
for (auto number : stack2 )
27+
console::write_line(number);
28+
29+
// Create an array twice the size of the stack and copy the elements of the stack, starting at the middle of the array.
30+
auto array2 = array<string>(numbers.count() * 2);
31+
numbers.copy_to(array2, numbers.count());
32+
33+
// Create a second stack, using the constructor that accepts an ienumerable<type_t>.
34+
auto stack3 = stack<string>(array2);
35+
36+
console::write_line("\nContents of the second copy, with duplicates and nulls:");
37+
for (auto number : stack3 )
38+
console::write_line(number);
39+
40+
console::write_line("\nstack2.contains(\"four\") = {0}", stack2.contains("four"));
41+
42+
console::write_line("\nstack2.clear()");
43+
stack2.clear();
44+
console::write_line("\nstack2.count() = {0}", stack2.count());
45+
}
46+
};
47+
}
48+
49+
startup_(examples::program::main);
50+
51+
// This code can produce the following output :
52+
//
53+
// five
54+
// four
55+
// three
56+
// two
57+
// one
58+
//
59+
// Popping 'five'
60+
// Peek at next item to destack: four
61+
// Popping 'four'
62+
//
63+
// Contents of the first copy:
64+
// one
65+
// two
66+
// three
67+
//
68+
// Contents of the second copy, with duplicates and nulls:
69+
// one
70+
// two
71+
// three
72+
//
73+
//
74+
//
75+
//
76+
// stack2.contains("four") = false
77+
//
78+
// stack2.clear()
79+
//
80+
// stack2.count() = 0

0 commit comments

Comments
 (0)