Lists C++ Standard template library list

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Singly linked lists Doubly linked lists
OSPF Header OSPF HEADER OSPF HEADER for this project Types we will use
In peer-to-peer networks such as gnutella, each host must search out other hosts. When a host finds another host, these hosts become neighbors. Often a.
Computer Science 1620 Loops.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
1 Standard Containers: Lists Gordon College Resource:
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Programming is instructing a computer to perform a task for you with the help of a programming language.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Fall Week 4 CSCI-141 Scott C. Johnson.  Computers can process text as well as numbers ◦ Example: a news agency might want to find all the articles.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Implementation of the Hangman Game in C++
CS 453 Computer Networks Lecture 9 Layer 2 – Data Link Layer.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Research Topics in Computational Science. Agenda Survey Overview.
Lecture 7 : Intro. to STL (Standard Template Library)
In peer-to-peer networks such as gnutella, each host must search out other hosts. When a host finds another host, these hosts become neighbors. Often a.
EIToolkit stub doc. main.cpp file int main(int argc, char* argv[]) { EIProperties::UseStandards(); // create the stub ExampleStub stub; stub.Start();
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Generic Programming and Library Design Brian Bartman
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
Lecture 36 OOP The STL Standard Template Library
Module 20/21/22: The Standard Template Library
C++ Lesson 1.
Iterators.
Basic concepts of C++ Presented by Prof. Satyajit De
Concepts of Programming Languages
Topic Pre-processor cout To output a message.
Introduction to Python
Lecture 7-2 : STL Iterators
Computer Programming Fundamentals
Class 11: Two-argument recursion
To do While (1) Receive hello Search for more neighbors?
Basic operators - strings
Programming for Engineers in Python
While Loops in Python.
Python - Loops and Iteration
Starting Out with C++ Early Objects Eighth Edition
Lecture 7-2 : STL Iterators
Today’s Learning Objective
File Handling Programming Guides.
Selection CSCE 121 J. Michael Moore.
TIPS: How to Be Successful
1. Open Visual Studio 2008.
Anatomy of a Function Part 1
Algorithm Discovery and Design
Iteration: Beyond the Basic PERFORM
CSS432 (Link Level Protocols) Reliable Transmission Textbook Ch 2.5
In peer-to-peer networks such as gnutella, each host must search out other hosts. When a host finds another host, these hosts become neighbors. Often a.
Chapter 6 Array-Based Lists.
If Statements.
STL Iterators Separating Container from Data Access.
Code Refresher Test #1 Topics:
Searching.
Lecture 8 : Intro. to STL (Standard Template Library)
Standard Template Library (STL)
Lecture 8-2 : STL Iterators and Algorithms
Then this information is wrong
Ruth Anderson CSE 160 University of Washington
While Loops in Python.
I am A I have heard: no one A B Have heard list Have heard list.
Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.
Presentation transcript:

Lists C++ Standard template library list Add the following to the top of cpp file #include <list> using namespace std; E.g., list of ints making a list of ints list<int> myList; Add an element to myList myList.push_back(1); myList.push_back(2); Iterate through the list and print each element for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) printf(“entry=%d\n”,*it);

Iterate through std::lists for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) printf(“entry=%d\n”,*it); for( int x : mylist) printf(“entry=%d\n”,x); for( int &x : mylist) x = x + 1; // int &x allows changes to x to be save in the list printf(“entry=%d\n”,x); // this will show the changes made above. for (auto &x : myList) // I forgot that myList is a list of integers. Or, I might change myLists later and don’t want to have to change all my other code

Removing elements from Lists Remove an element from the list int elementToRemove = 2; for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) { if (*it == elementToRemove) myList.erase(it); break; // not breaking would result in a crash } Alternatively myList.remove(1); // removes all elements == 1. But this requires == operator, which exists for int, but might not for other types. But == does exist for all classes used in this project

Example, Iterating through a list Recall, when a hello arrives, we need to check if thisHost is in the list of neighbors included in the hello message

A B A A I am A I have heard: no one Have heard list Have heard list Received message from A if thisHost is in the helloMessage’s list of neighbors isBidirectional = True else isBidirectional = False Add A to list of unidirectional neighbors Q: Why did we add A to the list of unidirectional neighbors and not the list of bidirectional neighbors? A: Because A has not heard any messages from B A: The hello message from A did not include B I am A I have heard: no one A B Have heard list Have heard list A Unidirectional Neighbors Bidirectional Neighbors Unidirectional Neighbors Bidirectional Neighbors A

Example, Iterating through a list Recall, when a hello arrives, we need to check if thisHost is in the list of neighbors included in the hello message Packet pkt; If (udpSocket.checkForNewPacket(pkt, 2)>0) { HelloMessage helloMessage; helloMessage.getFromPacket(pkt); //TODO: extract sender from helloMessage // TODO: check if sender is in list of bi/unidirectional neighbors // check if this host is in neighbor’s list of recently heard neighbors boolean isBidirectional = False; for(HostId &hostId : helloMessage.neighbors) { if (thisHost == hostId) { // yes it is isBidirectional = True; } Classes Packet, HelloMessage, UdpSocket, and HelloMessage are all provided Suppose that: helloMessage.neighors = {A,B,C} thisHost = C iteration1: hostId = A If (thisHost == hostId)  if (C == A)  False Iteration 2: hostId = B If (thisHost == hostId)  if (C == B)  False Iteration 3: hostId = C If (thisHost == hostId)  if (C==C)  True isBidirectional = True

Example, Iterating through a list Recall, when a hello arrives, the time that a hello was last received from the neighbor should be updated Packet pkt; If (udpSocket.checkForNewPacket(pkt, 2)>0) { HelloMessage helloMessage; helloMessage.getFromPacket(pkt); //TODO: …. for (NeighborInfo &neigbor : bidirectionalNeighbors) { if (neighbor == helloMessage.source) { neighbor. updateTimeToCurrentTime(); } Suppose that: bidirectionalNeighors = {A,B,C} helloMessage.source = C iteration1: neighbor = A If (neighbor == helloMessage.source)  if (A == C)  False Iteration 2: neighbor = B If (neighbor == helloMessage.source)  if (B == C)  False Iteration 3: neighbor = C If (neighbor == helloMessage.source)  if (C == C)  True C. updateTimeToCurrentTime();

Example, Iterating through a list for (NeighborInfo &neigbor : bidirectionalNeighbors) { if (neighbor == helloMessage.source) { neighbor. updateTimeToCurrentTime(); } Suppose that: bidirectionalNeighors = {A,B,C} helloMessage.source = C iteration1: neighbor = A If (neighbor == helloMessage.source)  if (A == C)  False Iteration 2: neighbor = B If (neighbor == helloMessage.source)  if (B == C)  False Iteration 3: neighbor = C If (neighbor == helloMessage.source)  if (C == C)  True C. updateTimeToCurrentTime(); C. timeWhenLastHelloArrived = current time Wait, did we just change timeWhenLastHelloArrived in some object, or did we change it in the list?

Example, Iterating through a list for (NeighborInfo &neigbor : bidirectionalNeighbors) { if (neighbor == helloMessage.source) { neighbor. updateTimeToCurrentTime(); } Suppose that: bidirectionalNeighors = {{hostId=A, timeWhenLastHelloArrived=12:00}, {hostId=B, timeWhenLastHelloArrived=12:01}, {hostId=C; timeWhenLastHelloArrived=12:00}} helloMessage.source = C iteration1: neighbor = {hostId=A, timeWhenLastHelloArrived=12:00} If (neighbor == helloMessage.source)  if (A == C)  False Iteration 2: neighbor = {hostId=B, timeWhenLastHelloArrived=12:01} If (neighbor == helloMessage.source)  if (B == C)  False Iteration 3: neighbor = {hostId=C, timeWhenLastHelloArrived=12:00} If (neighbor == helloMessage.source)  if (C == C)  True C. updateTimeToCurrentTime(); C. timeWhenLastHelloArrived = current time Wait, did we just change timeWhenLastHelloArrived in some object, or did we change it in the list? for (NeighborInfo &neigbor : bidirectionalNeighbors) for (NeighborInfo neigbor : bidirectionalNeighbors) Yes, we are working with the ACTUAL objects in the list No, we are working with COPIES objects in the list

Example, Iterating through a list for (NeighborInfo &neigbor : bidirectionalNeighbors) { if (neighbor == helloMessage.source) { neighbor. updateTimeToCurrentTime(); }

Example, Iterating through a list Recall, sometimes we might need to remove an element from a list // check if neighbor has timed out time(&currentTime); // get current time for(NeighborInfo &neighbor : unidirectionalNeighbors) { if (difftime(currentTime, neighbor.timeWhenLastHelloArrived) > 40) { // remove old neighbor unidirectionalNeighbors .remove(neighbor); break; } Without the break, your code will crash list item list item list item list item