c++ - 如何访问 std::queue 数据结构的成员元素?
问题描述
使用 Visual Studio 2019 在 C+ 中编码,我定义了一个结构。我正在创建该数据结构的队列,并将 2 个元素推入队列。现在的问题是如何访问队列内部结构元素的成员?任何指导表示赞赏!
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <queue>
typedef struct _myqueuestruct
{
string name;
int citypin;
int employeeId;
}myqueuestruct;
int main()
{
queue<myqueuestruct> myQ;
myqueuestruct myQelement;
myQelement.name = "Harry";
myQelement.citypin = "Ohio";
myQelement.employeeId = "345";
// Insert some elements into the queue
myQ.push(myQelement);
myQelement.name = "John";
myQelement.citypin = "Jaipur";
myQelement.employeeId = "223";
// Insert some elements into the queue
myQ.push(evtSvcElement);
//myQ.size();
//queue<myqueuestruct>::iterator it = myQ.begin();
for (int i = 0; i < myQ.size(); i++)
{
cout << myQ.front();
myQ.pop(); //???? How do I access the member values of the elements of the queue?
}
while (1);
return 0;
}
解决方案
好吧,front
返回对第一个元素的引用,就像这样:
std::cout << myQ.front().name; // and similarly for other elements
或者,例如,自己做一个参考:
auto& ref = myQ.front();
ref.name = "foo";
ref.citypin = 42;
// etc.
推荐阅读
- java - 为什么 Symbol 的字段(左右)总是返回 0?
- events - 谷歌日历 API 问题(已删除项目)
- wix - 如果已安装应用程序,如何创建自定义错误对话框?
- angular - 无法使用日期字符串对物料表列进行排序
- python - python中的数据表示差异
- html - 无法用鼠标单击列表中的第一个单选按钮,但可以使用向上和向下箭头键选择它
- java - 是否可以从对象数组中删除索引?
- python - 使用 python 将 [abc] 转换为 [a,b,c]
- django - DRF 序列化程序错误:AttributeError:“FeedPostSerializer”对象没有属性“auth_user”
- css - Webpack CSS 输出总是最小化