简体   繁体   中英

how do i store and get a queue of structure?

How do I store and get value of structure into a queue where I have 2 values? I wanna queue this few values.

| 4 , 2 |

| 3, 5 |

Where by 4,2 and 3,5 can be a variable such as

primaryQ.push(a, b);

My structure:

struct primarytemp{
    double process;
    double secondary;
};

Here's how I declared my main:

int main(){
    queue<primarytemp> primaryQ;
    primaryQ.push(4, 2);
}

You can just push a primarytemp object into the queue, without modifications to the type:

queue<primarytemp> q;

q.push(primarytemp{4., 2.});  // Requires current C++ standard (C++11)

primarytemp p = {3., 5.};
q.push(p);                    // Also  works pre-C++11

As has been mentioned by others, you can add a constructor to primarytemp , although this means it is no longer an aggregate. This may or may not matter.

primarytemp
{
  primarytemp() : process(), secondary() {}
  primarytemp(double process, double secondary) 
  : process(process), secondary(secondary) {}

  double process;
  double secondary; 
};

This allows you to say

q.push(primarytemp(4., 2.)); // Also works pre-C++11

To access the element you just pushed, use the back() method, which returns reference:

std::cout << "process" << q.back().process() << std::endl;

You can make a copy of that element too:

primarytemp = q.back();

The front() method allows you to do the same with the first element in the queue.

To remove the element at the front:

q.pop();

If you are using C++, you could use pair<double, double> instead of manually creating a queue. This is how to declare it and use it:

queue< pair<double, double> > primaryQ;
pair<double, double> myPair;
myPair = make_pair(3, 5);

cout << myPair.first;//will print 3, the first element of the couple
cout << mtPair.second//will print 5, the second element of the couple

primaryQ.push(myPair);//push the pair into the queue

You can also insert it like this:

primaryQ.push( make_pair(3, 5) );

As it is C++ struct has constructor, so you could:

struct primarytemp
{
    primarytemp(double p, double s) : process(p), secondary(s)
    {}

    double process;
    double secondary;
};

And:

int main(){
    queue<primarytemp> primaryQ;
    primaryQ.push(primarytemp(4, 2));
}

Also if you have only two values, you can use pair<double, double> , struct is good in case you have more data.

If your compiler provides C++11 features you can use std::queue::emplace(...) method. Emplace() allows you to create element of queue by forwarding its argument list directly to constructor of queue type. See code below:

struct primarytemp
{
    double process;
    double secondary;

    primarytemp(double aProcess, double aSecondary) 
        : process(aProcess), secondary(aSecondary) {}
};

int main()
{
    queue<primarytemp> primaryQ;
    primaryQ.emplace(4.0, 2.0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM