From 1cc032413dd29dfd5e80535884b4a8a5e6c0cece Mon Sep 17 00:00:00 2001 From: Trance-0 <60459821+Trance-0@users.noreply.github.com> Date: Mon, 17 Feb 2025 10:10:51 -0600 Subject: [PATCH] update names --- pages/CSE332S/CSE332S_L1.md | 3 +- pages/CSE332S/CSE332S_L10.md | 2 +- pages/CSE332S/CSE332S_L11.md | 2 +- pages/CSE332S/CSE332S_L2.md | 2 +- pages/CSE332S/CSE332S_L3.md | 2 +- pages/CSE332S/CSE332S_L4.md | 2 +- pages/CSE332S/CSE332S_L5.md | 2 +- pages/CSE332S/CSE332S_L6.md | 2 +- pages/CSE332S/CSE332S_L7.md | 2 +- pages/CSE332S/CSE332S_L8.md | 2 +- pages/CSE332S/CSE332S_L9.md | 200 ++++++++++++++++++++++++++++++++++- pages/CSE332S/_meta.js | 32 ++---- 12 files changed, 217 insertions(+), 36 deletions(-) diff --git a/pages/CSE332S/CSE332S_L1.md b/pages/CSE332S/CSE332S_L1.md index 2a6bbaa..e439a35 100644 --- a/pages/CSE332S/CSE332S_L1.md +++ b/pages/CSE332S/CSE332S_L1.md @@ -1,5 +1,4 @@ -# Lecture 1 - +# CSE332S Lecture 1 ## Today: diff --git a/pages/CSE332S/CSE332S_L10.md b/pages/CSE332S/CSE332S_L10.md index 72b00ef..8e77085 100644 --- a/pages/CSE332S/CSE332S_L10.md +++ b/pages/CSE332S/CSE332S_L10.md @@ -1 +1 @@ -# Lecture 10 \ No newline at end of file +# CSE332S Lecture 10 \ No newline at end of file diff --git a/pages/CSE332S/CSE332S_L11.md b/pages/CSE332S/CSE332S_L11.md index 9cdf10f..4474799 100644 --- a/pages/CSE332S/CSE332S_L11.md +++ b/pages/CSE332S/CSE332S_L11.md @@ -1 +1 @@ -# Lecture 11 \ No newline at end of file +# CSE332S Lecture 11 \ No newline at end of file diff --git a/pages/CSE332S/CSE332S_L2.md b/pages/CSE332S/CSE332S_L2.md index 322afd7..37f48ec 100644 --- a/pages/CSE332S/CSE332S_L2.md +++ b/pages/CSE332S/CSE332S_L2.md @@ -1,4 +1,4 @@ -# Lecture 2 +# CSE332S Lecture 2 Today we'll talk generally about C++ development (plus a few platform specifics): diff --git a/pages/CSE332S/CSE332S_L3.md b/pages/CSE332S/CSE332S_L3.md index ab5ebd7..2ff5f02 100644 --- a/pages/CSE332S/CSE332S_L3.md +++ b/pages/CSE332S/CSE332S_L3.md @@ -1,4 +1,4 @@ -# Lecture 3 +# CSE332S Lecture 3 ## C++ basic data types diff --git a/pages/CSE332S/CSE332S_L4.md b/pages/CSE332S/CSE332S_L4.md index 3c16f3d..ac4faa1 100644 --- a/pages/CSE332S/CSE332S_L4.md +++ b/pages/CSE332S/CSE332S_L4.md @@ -1,4 +1,4 @@ -# Lecture 4 +# CSE332S Lecture 4 ## Namespace details diff --git a/pages/CSE332S/CSE332S_L5.md b/pages/CSE332S/CSE332S_L5.md index 07d8670..804b627 100644 --- a/pages/CSE332S/CSE332S_L5.md +++ b/pages/CSE332S/CSE332S_L5.md @@ -1,4 +1,4 @@ -# Lecture 5 +# CSE332S Lecture 5 ## Function and the Call Stack diff --git a/pages/CSE332S/CSE332S_L6.md b/pages/CSE332S/CSE332S_L6.md index 53d199a..eebd00b 100644 --- a/pages/CSE332S/CSE332S_L6.md +++ b/pages/CSE332S/CSE332S_L6.md @@ -1,4 +1,4 @@ -# Lecture 6 +# CSE332S Lecture 6 ## Expressions diff --git a/pages/CSE332S/CSE332S_L7.md b/pages/CSE332S/CSE332S_L7.md index 7bf8da5..13fe0d1 100644 --- a/pages/CSE332S/CSE332S_L7.md +++ b/pages/CSE332S/CSE332S_L7.md @@ -1,4 +1,4 @@ -# Lecture 7 +# CSE332S Lecture 7 ## Debugging diff --git a/pages/CSE332S/CSE332S_L8.md b/pages/CSE332S/CSE332S_L8.md index b1d9c20..2a8a27c 100644 --- a/pages/CSE332S/CSE332S_L8.md +++ b/pages/CSE332S/CSE332S_L8.md @@ -1,4 +1,4 @@ -# Lecture 8 +# CSE332S Lecture 8 ## From procedural to object-oriented programming diff --git a/pages/CSE332S/CSE332S_L9.md b/pages/CSE332S/CSE332S_L9.md index 5b73bd5..259365c 100644 --- a/pages/CSE332S/CSE332S_L9.md +++ b/pages/CSE332S/CSE332S_L9.md @@ -1 +1,199 @@ -# Lecture 9 \ No newline at end of file +# CSE332S Lecture 9 + +## Sequential Containers + +Hold elements of a parameterized type (specified when the container variable is +declared): `vector v; vector v1;` + +Elements are inserted/accessed based on their location (index) + +- A single location cannot be in more than 1 container +- Container owns elements it contains - copied in by value, contents of container are destroyed when the container is destroyed + +Containers provide an appropriate interface to add, remove, and access elements + +- Interface provided is determined by the specifics of the container - underlying data structure + +_usually the provided interface of the container runs in constant time_ + +### Non-random access containers + +Cannot access elements in constant time, must traverse the container to get to the desired element. + +#### Forward list + +- implemented as a singly linked list of elements +- Elements are not contiguous in memory (no random access) +- Contains a pointer to the first element (can only grow at front, supplies a `forward_iterator`) + +#### List + +- implemented as a doubly linked list of elements +- Elements are not contiguous in memory (no random access) +- Contains a pointer to front and back (can grow at front or back, supplies a `bidirectional_iterator`) + +### Random access containers + +Add, remove, and access elements in constant time. + +#### Vector + +- implemented as a dynamically sized array of elements +- Elements are contiguous in memory (random access) +- Can only grow at the back via `push_back()` (amortized constant time, _may expand the array takes linear time_) + +#### Deque + +- double-ended queue of elements +- Elements do not have to be contiguous in memory, but must be accessible in constant time (random access) +- Can grow at front or back of the queue + +## Iterators and iterator types + +Could use the subscript/indexing (operator[]) operator with a loop + +- Not all containers supply an [] operator, but we should still be able to traverse and access their elements + +Containers provide iterator types: + +- `vector::iterator i; // iterator over non-const elements` +- `vector::const_iterator ci; // iterator over const elements` + +Containers provide functions for creating iterators to the beginning and just past +the end of the container: + +```cpp +vector v = { 1, 2, 3, 4, 5 }; +auto start = v.cbegin(); // cbegin() gives const iterator, you can't modify the elements, you can use .begin() to get a non-const iterator +while (start != v.cend()) { // over const elements, v.cend() is not a valid element, it's just one pass the end. + cout << *start << endl; + ++start; +} +``` + +### More on iterators + +- Iterators generalize different uses of pointers + - Most importantly, define left-inclusive intervals over the ranges of elements in a container `[begin, end)` +- Iterators interface between algorithms and data structures (Iterator design pattern) + - Algorithms manipulate iterators, not containers +- An iterator’s value can represent 3 kinds of `states`: + - `dereferencable` (points to a valid location in a range), eg `*start` + - `past the end` (points just past last valid location in a range), eg `v.cend()` + - `singular` (points to nothing), eg `nullptr` +- Can construct, compare, copy, and assign iterators so that native and library types + can inter-operate + +### Properties of Iterator Intervals + +- Valid intervals can be traversed safely with an iterator + - An empty range `[p,p)` is valid + - If `[first, last)` is valid and non-empty, then `[first+1, last)` is also valid + - Proof: iterative induction on the interval + - If `[first, last)` is valid + - and position `mid` is reachable from `first` + - and `last` is reachable from `mid` + - then `[first, mid)` and `[mid, last)` are also valid + - Proof: divide and conquer induction on the interval + - If `[first, mid)` and `[mid, last)` are valid, then `[first, last)` is valid + - Proof: divide and conquer induction on the interval + +### Interface supplied by different iterator types + +- Output iterators: used in output operations (write), + - "destructive" read at head of stream (istream) +- Input iterators: used in input operations (read), + - "transient" write to stream (ostream) +- Forward iterators: used in forward operations (read, write), common used in forward linked list + - Value _persists_ after read/write +- Bidirectional iterators: used in bidirectional operations (read, write), common used in doubly linked list + - Value have _locations_ +- Random access iterators: used in random access operations (read, write), common used in vector + - Can express _distance_ between two iterators + +| Category/Operation | Output | Input | Forward | Bidirectional | Random Access | +| ------------------ | -------------- | -------------- | -------------- | -------------- | ----------------- | +| Read | N/A | `=*p`(r-value) | `=*p`(r-value) | `=*p`(r-value) | `=*p`(r-value) | +| Access | N/A | `->` | `->` | `->` | `->,[]` | +| Write | `*p=`(l-value) | N/A | `*p=`(l-value) | `*p=`(l-value) | `*p=`(l-value) | +| Iteration | `++` | `++` | `++` | `++,--` | `++,--,+,-,+=,-=` | +| Comparison | N/A | `==,!=` | `==,!=` | `==,!=` | `==,!=,<,>,<=,>=` | + +## Generic algorithms in CPP + +A standard collection of generic algorithms + +- Applicable to various types and containers + - E.g., sorting integers (`int`) vs. intervals (`pair`) + - E.g., sorting elements in a `vector` vs. in a C-style array +- Polymorphic even without inheritance relationships - interface polymorphism + - Types substituted need not have a common base class + - Must only provide the interface the algorithm requires + - Common iterator interfaces allow algorithms to work with many types of +containers, without knowing the implementation details of the container +- Significantly used with the sequence containers + - To reorder elements within a container’s sequence + - To store/fetch values into/from a container + - To calculate various values and properties from it + +### Organization of C++ Algorithm Libraries + +The `` header file contains + +- Non-modifying sequence operations + - Do some calculation but don’t change sequence itself + - Examples include `count`, `count_if` +- Mutating sequence operations + - Modify the order or values of the sequence elements + - Examples include `copy`, `random_shuffle` +- Sorting and related operations + - Modify the order in which elements appear in a sequence + - Examples include `sort`, `next_permutation` +- The `` header file contains + - General numeric operations + - Scalar and matrix algebra, especially used with `vector` + - Examples include `accumulate`, `inner_product` + +### Using Algorithms + +Example using `std::sort()` + +- `sort` algorithm + - Reorders a given range + - Can also plug in a functor to change the ordering function + - http://www.cplusplus.com/reference/algorithm/sort/ + +- Requires random access iterators. +- Requires elements being sorted implement operator `<` (less than) + +```cpp +#include +#include +#include + +using namespace std; + +int main(int argc, char* argv[]) { + vector v = { 3, 1, 4, 1, 5, 9 }; + sort(v.begin(), v.end()); // sort the vector + for (int i : v) { + cout << i << " "; + } + return 0; +} +``` + +Sort forward list of strings + +```cpp +forward_list fl = { "hello", "world", "this", "is", "a", "test" }; +sort(fl.begin(), fl.end()); +``` +**This is not valid because forward list does not support random access iterators.** + +Sort vector of strings + +```cpp +vector v = { "hello", "world", "this", "is", "a", "test" }; +sort(v.begin(), v.end()); +``` \ No newline at end of file diff --git a/pages/CSE332S/_meta.js b/pages/CSE332S/_meta.js index 3c53b12..d90f7f9 100644 --- a/pages/CSE332S/_meta.js +++ b/pages/CSE332S/_meta.js @@ -6,28 +6,12 @@ export default { CSE332S_L1: "Object-Oriented Programming Lab (Lecture 1)", CSE332S_L2: "Object-Oriented Programming Lab (Lecture 2)", CSE332S_L3: "Object-Oriented Programming Lab (Lecture 3)", - CSE332S_L4: { - display: "hidden" - }, - CSE332S_L5: { - display: "hidden" - }, - CSE332S_L6: { - display: "hidden" - }, - CSE332S_L7: { - display: "hidden" - }, - CSE332S_L8: { - display: "hidden" - }, - CSE332S_L9: { - display: "hidden" - }, - CSE332S_L10: { - display: "hidden" - }, - CSE332S_L11: { - display: "hidden" - }, + CSE332S_L4: "Object-Oriented Programming Lab (Lecture 4)", + CSE332S_L5: "Object-Oriented Programming Lab (Lecture 5)", + CSE332S_L6: "Object-Oriented Programming Lab (Lecture 6)", + CSE332S_L7: "Object-Oriented Programming Lab (Lecture 7)", + CSE332S_L8: "Object-Oriented Programming Lab (Lecture 8)", + CSE332S_L9: "Object-Oriented Programming Lab (Lecture 9)", + CSE332S_L10: "Object-Oriented Programming Lab (Lecture 10)", + CSE332S_L11: "Object-Oriented Programming Lab (Lecture 11)", }