-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (129 loc) · 4.19 KB
/
Copy pathmain.cpp
File metadata and controls
189 lines (129 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Language review is complete
// Now begin study of data structures
// What's a Data Structure?
// an object that stores a collection of other objects
// data structures are sometimes called "containers"
// What are the common data types?
// List
// Set
// Map
// Stack
// Queue
// Priority Queue
// What's our approach to studying data structures?
// 1. learn the operations and behavior of each type
// 2. learn how they can be used to solve problems
// 3. first use the data types in the standard library
// 4. later learn how each type is implemented
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include "print.h"
using namespace std;
vector<string> reverse(vector<string> items) {
return items;
}
int main() {
// List Data Type (4.1, 4.6, 4.9)
// What's a List?
// 1. a container that holds objects
// 2. each object has an index or position
// 3. objects are kept in the order given by the user
// What operations are supported by a List?
// What's the abstract interface for a List?
// size();
// append(item);
// remove(index);
// insert(index, item);
// get(index);
// set(index, item);
// What happens to the item at index 2 when the item at index 1 is removed?
// What happens to the item at index 2 when an item is inserted at index 2?
// Classwork
// You may work with a partner.
// What does the program print?
// List L1;
// L1.append(1);
// L1.append(2);
// L1.append(4);
// print(L1);
//
// List L2;
// for (int i = 0; i < L1.size(); i++)
// L2.insert(0, L1.get(i));
// print(L2);
//
// for (int i = 0; i < L1.size(); i++)
// L2.insert(2 * i, L1.get(i));
// print(L2);
//
// for (int i = 0; i < L1.size(); i++)
// L2.set(i, L1.get(i));
// print(L2);
//
// int len = L2.size();
// for (int i = 0; i < len; i++)
// L2.remove(i);
// print(L2);
// Lists in the C++ Library
//#include <vector>
// std::vector
//#include <list>
// std::list
// What are some of the operations supported by std::vector?
// size_t size() const;
// void push_back(const ItemType &item); // append
// iterator erase(iterator index);
// iterator insert(iterator index, const ItemType &item);
// ItemType &at(int index);
// ItemType &operator[](int index);
// DEMO (write reverse method using indexes)
vector<string> names;
names.emplace_back("Mary");
names.emplace_back("Jane");
names.emplace_back("Mark");
names.emplace_back("Mark");
names.emplace_back("John");
// print(names);
//
// print(reverse(names));
// What are some of the operations supported by std::list?
// size_t size() const;
// void push_back ( const ItemType& item );
// iterator erase ( iterator index );
// iterator insert ( iterator index, const ItemType& item );
// DEMO (change vector to list)
// Iterators
// How do you iterate over an array?
// for (int i = 0; i < length; i++)
// print(array[i]);
// How do you iterate over a list?
// Why do you need an Iterator?
// Why can't you use indexing?
// some containers don't support indexing
// internal structure can't be indexed efficiently
// What's an Iterator?
// a pointer that steps through the objects in a container
// What operations are supported by an Iterator?
// What's the abstract interface for an Iterator?
// create();
// access();
// increment();
// compare();
// How do you create and use an Iterator on a list?
// list<int> items;
// list<int>::iterator i = items.begin();
// while (i != items.end()) {
// cout << *i;
// i++;
// }
// DEMO (rewrite reverse using iterators)
// Why List?
// A 'vector' can do most anything a 'list' can do.
// A 'list' cannot do indexing, while a 'vector' can.
// Why is a 'list' useful?
// DEMO (time for insert on vector vs list)
}