// Native_STL_ListTemplate.cpp : Defines the entry point for the console application. /* _____________________________________________________ T H E S T A N D A R D T E M P L A T E L I B R A R Y: L I S T S Ron Kessler Created 1/23/2017 Native Code Version Run with CTL-F5 Some thoughts: This Standard Template Library contains a built-in version of the Linked List! Any time you create a list by using a structure that contains a pointer to the next element, it is called a linked list. Each object in a linked list is called a node. Each node contains a value (like an array element) and a pointer to the next node. That is how it connects to the other elements. Think of it like a train. We add cars to the train and the pointer is like the coupler. It knows the memory location of the next node. Allain, Alex. Jumping into C++ (p. 204). Cprogramming.com. Kindle Edition. To set one up you simply do this: list myNewList; We use T as an alias like we do in setting up parameters in a method. The LIST template allows us to store almost any type of data and then use an interator from the STL also, to cycle through any collection of data. Instead of a sort method for arrays, another one for lists, etc. we just use one iterator class to handle any type of data we throw at it! The STL has algorithms like sort, accumulate, consecutive, and find. Use accumulate to get a running total: int myTotal = accumulate(begin(myIntegersArray), end(myIntegersArray), 0); //The 0 is the initial value of myTotal Use consectutive to fill an array with ints say from 0 - 10000. It saves massive typing/time. So these algorithms can operate on any type of data that supports them (collections do) and you can see that this avoids using a for loop to get our running total. Algorithms are built-in functions store in a library called the STL. Iterators are built-in objects designed to cycle through our data. The accumulate example does not care what type of data it is working on. It is like a generic function. Advantage/Disadvantage of Lists vs Arrays and Vectors 1. Can easily grow/shrink in size 2. Can insert/delete nodes faster than Vectors 3. The primary advantage of an array over a linked list is that an array lets you choose any element very quickly, just by providing its index. A linked list, on the other hand, requires that you look through every element in the list until you find the one you want. Allain, Alex. Jumping into C++ (p. 210). Cprogramming.com. Kindle Edition. This program demonstrates the STL list container and a few of the common methods it has. */ #include "stdafx.h" #include #include // Include the list header using namespace std; int main () { // Define a list object that will hold int data list myList; // Define an iterator for the list. Iterators cycle through collections. //Iterators have a begin and end method so we don't need to know the size list::iterator iter; // Add values to the list so we have something to view for (int x = 0; x < 100; x += 10) myList.push_back (x); // Display the values for (iter = myList.begin (); iter != myList.end (); iter++) cout << *iter << " "; //dereferencing the iterator to get the value? cout << endl; // Now reverse the order of the elements with the built-in reverse method myList.reverse (); // Display the values again for (iter = myList.begin (); iter != myList.end (); iter++) cout << *iter << " "; cout << endl; return 0; } /* Some STL methods: list.back returns a reference to the last element list.front returns a reference to the first element pop_back removes the last element from the list pob_front removes the first element push_back(x) inserts an element with the value of x at the end of the list push_front(x) inserts at the beginning reverse swaps the order of all the items size gives us the # of elemeents in the list Other STL Algorithms: Using find to get a value from a vector.... auto seventySeven = find(begin(doubles), end(doubles), 77.2); find which element contains 77.2 *seventySeven = 50; now that element = 50 by deferencing the iterator just like a pointer. Using sort to sort a vector: sort(begin(myVector), end(myVector)); */