[英]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? 如何将结构的值存储并获取到我有2个值的队列中? I wanna queue this few values. 我想排队这几个值。
| | 4 , 2 | 4,2 |
| | 3, 5 | 3、5 |
Where by 4,2 and 3,5 can be a variable such as 其中4,2和3,5可以是一个变量,例如
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: 您可以将primarytemp
对象推入队列,而无需修改类型:
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. 正如其他人所提到的,您可以向primarytemp
添加一个构造primarytemp
,尽管这意味着它不再是聚合。 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: 要访问刚刚推送的元素,请使用back()
方法,该方法返回引用:
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. front()
方法允许您对队列中的第一个元素执行相同的操作。
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. 如果使用的是C ++,则可以使用pair<double, double>
代替手动创建队列。 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: 由于它是C ++ struct
,因此具有构造函数,因此您可以:
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. 另外,如果只有两个值,则可以使用pair<double, double>
,如果您有更多数据,则struct
很好。
If your compiler provides C++11 features you can use std::queue::emplace(...)
method. 如果您的编译器提供C ++ 11功能,则可以使用std::queue::emplace(...)
方法。 Emplace() allows you to create element of queue by forwarding its argument list directly to constructor of queue type. Emplace()允许您通过将其参数列表直接转发到队列类型的构造函数来创建队列元素。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.