samedi 4 mars 2017

Storing value in vector C++

I'm using a vector to store values in this fashion:

std::vector v;

    if (type_of_packet == 1) //data
    {
            if (Pkt_no_last_seen_by_node1 < Pkt_no_being_sent_by_node0)
            {
                    NS_LOG_UNCOND(Simulator::Now ().GetSeconds () << " The packet received by 1 is a *UNIQUE* DATA packet.");
                    Pkt_no_last_seen_by_node1 = Pkt_no_being_sent_by_node0;
                    Pkt_no_being_sent_by_node1 = Pkt_no_last_seen_by_node1;
                    Node1SendAck = 1;
                    // strip packet contents and transfer contents to variables
                    node1_isbufferempty = header.Getisbufferempty ();  // header fields
                    node1_head = header.Gethead ();  // header fields
                    node1_tail = header.Gettail ();  // header fields

                    if (node1_head == node1_tail){

                    v.push_back(node1_head);

                    }

                    else {

                    // otherwise node1_head must be greater than node_tail -> in this case we store head, tail and head-1
                    v.push_back(node1_head);
                    v.push_back(node1_tail); 
                    v.push_back(node1_head-1);

                    }

The issue I have is, is that the data is not being kept each time this code is ran. Only the latest item is stored; whereas I'd like to store all data in the vector.

I'm aware when using std::vector you're not meant to worry about data storage from what I've gathered. And I cannot use the other way of using vectors in C++ as I'm writing some code in NS3 (//www.nsnam.org) Which will throw an error and force me to use std::vector.

Thanks!

Aucun commentaire:

Enregistrer un commentaire