Download presentation
Presentation is loading. Please wait.
1
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);
2
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
3
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
4
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
5
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
6
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
7
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();
8
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?
9
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
10
Example, Iterating through a list
for (NeighborInfo &neigbor : bidirectionalNeighbors) { if (neighbor == helloMessage.source) { neighbor. updateTimeToCurrentTime(); }
11
Example, Iterating through a list
Recall, sometimes we might need to remove an element from a list // check if neighbor has timed out time(¤tTime); // 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.