Lists C Standard template library list Add the
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> my. List; – Add an element to my. List. push_back(1); my. List. push_back(2); my. List. push_back(1); – Iterate through the list and print each element for( list<int>: : iterator it=my. List. begin(); it!=my. List. end(); ++it) printf(“entry=%dn”, *it);
Iterate through std: : lists • for( list<int>: : iterator it=my. List. begin(); it!=my. List. end(); ++it) – printf(“entry=%dn”, *it); • for( int x : mylist) – printf(“entry=%dn”, x); • for( int &x : mylist) – x = x + 1; – // int &x allows changes to x to be save in the list • for( int &x : mylist) – printf(“entry=%dn”, x); // this will show the changes made above. • for (auto &x : my. List) – // I forgot that my. List is a list of integers. Or, I might change my. Lists later and don’t want to have to change all my other code – printf(“entry=%dn”, x);
Removing elements from Lists Remove an element from the list int element. To. Remove = 2; for( list<int>: : iterator it=my. List. begin(); it!=my. List. end(); ++it) { if (*it == element. To. Remove) { my. List. erase(it); break; // not breaking would result in a crash } } – Alternatively my. List. 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 this. Host is in the list of neighbors included in the hello message
• Received message from A • if this. Host is in the hello. Message’s list of neighbors • is. Bidirectional = True • else • is. Bidirectional = 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 A Unidirectional Neighbors Bidirectional Neighbors Unidirectional Neighbors A Bidirectional Neighbors
Example, Iterating through a list Recall, when a hello arrives, we need to check if this. Host is in the list of neighbors included in the hello message Packet pkt; If (udp. Socket. check. For. New. Packet(pkt, 2)>0) { Hello. Message hello. Message; hello. Message. get. From. Packet(pkt); //TODO: extract sender from hello. Message // 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 is. Bidirectional = False; for(Host. Id &host. Id : hello. Message. neighbors) { if (this. Host == host. Id) { // yes it is is. Bidirectional = True; } } } Classes Packet, Hello. Message, Udp. Socket, and Hello. Message are all provided Suppose that: • hello. Message. neighors = {A, B, C} • this. Host = C • • • iteration 1: host. Id = A • If (this. Host == host. Id) if (C == A) False Iteration 2: host. Id = B • If (this. Host == host. Id) if (C == B) False Iteration 3: host. Id = C • If (this. Host == host. Id) if (C==C) True • is. Bidirectional = 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 (udp. Socket. check. For. New. Packet(pkt, 2)>0) { Hello. Message hello. Message; hello. Message. get. From. Packet(pkt); //TODO: …. for (Neighbor. Info &neigbor : bidirectional. Neighbors) { if (neighbor == hello. Message. source) { neighbor. update. Time. To. Current. Time(); } } } Suppose that: • bidirectional. Neighors = {A, B, C} • hello. Message. source = C • • • iteration 1: neighbor = A • If (neighbor == hello. Message. source) if (A == C) False Iteration 2: neighbor = B • If (neighbor == hello. Message. source) if (B == C) False Iteration 3: neighbor = C • If (neighbor == hello. Message. source) if (C == C) True • C. update. Time. To. Current. Time();
Example, Iterating through a list for (Neighbor. Info &neigbor : bidirectional. Neighbors) { if (neighbor == hello. Message. source) { neighbor. update. Time. To. Current. Time(); } } Suppose that: • bidirectional. Neighors = {A, B, C} • hello. Message. source = C • • • iteration 1: neighbor = A • If (neighbor == hello. Message. source) if (A == C) False Iteration 2: neighbor = B • If (neighbor == hello. Message. source) if (B == C) False Iteration 3: neighbor = C • If (neighbor == hello. Message. source) if (C == C) True • C. update. Time. To. Current. Time(); • C. time. When. Last. Hello. Arrived = current time Wait, did we just change time. When. Last. Hello. Arrived in some object, or did we change it in the list?
Example, Iterating through a list for (Neighbor. Info &neigbor : bidirectional. Neighbors) { if (neighbor == hello. Message. source) { neighbor. update. Time. To. Current. Time(); } } Suppose that: • bidirectional. Neighors = {{host. Id=A, time. When. Last. Hello. Arrived=12: 00}, {host. Id=B, time. When. Last. Hello. Arrived=12: 01}, {host. Id=C; time. When. Last. Hello. Arrived=12: 00}} • hello. Message. source = C • iteration 1: neighbor = {host. Id=A, time. When. Last. Hello. Arrived=12: 00} • If (neighbor == hello. Message. source) if (A == C) False • Iteration 2: neighbor = {host. Id=B, time. When. Last. Hello. Arrived=12: 01} • If (neighbor == hello. Message. source) if (B == C) False • Iteration 3: neighbor = {host. Id=C, time. When. Last. Hello. Arrived=12: 00} • If (neighbor == hello. Message. source) if (C == C) True • C. update. Time. To. Current. Time(); • C. time. When. Last. Hello. Arrived = current time Wait, did we just change time. When. Last. Hello. Arrived in some object, or did we change it in the list? for (Neighbor. Info &neigbor : bidirectional. Neighbors) Yes, we are working with the ACTUAL objects in the list for (Neighbor. Info neigbor : bidirectional. Neighbors) No, we are working with COPIES objects in the list
Example, Iterating through a list for (Neighbor. Info &neigbor : bidirectional. Neighbors) { if (neighbor == hello. Message. source) { neighbor. update. Time. To. Current. Time(); } }
Example, Iterating through a list Recall, sometimes we might need to remove an element from a list // check if neighbor has timed out time(¤t. Time); // get current time for(Neighbor. Info &neighbor : unidirectional. Neighbors) { if (difftime(current. Time, neighbor. time. When. Last. Hello. Arrived) > 40) { // remove old neighbor unidirectional. Neighbors. remove(neighbor); break; } } Without the break, your code will crash list item
- Slides: 11