[英]Unable to access contents of initialized struct from a vector
I have a struct: 我有一个结构:
typedef struct
{
Qt::Key qKey;
QString strFormType;
} KeyPair;
Now I initialize KeyPair instantiations so I could use it for my Automated Test App. 现在,我初始化KeyPair实例,以便可以将其用于我的自动测试应用程序。
KeyPair gTestMenu[] =
{
{ Qt::Key_1 , "MyForm" },
{ Qt::Key_1 , "SubForm" },
{ Qt::Key_Escape, "DesktopForm" }
};
KeyPair gBrowseMenu[] =
{
{ Qt::Key_1 , "MyForm" },
{ Qt::Key_2 , "Dialog" },
{ Qt::Key_Escape, "DesktopForm" }
};
and like 100 more instantiations....
Currently, I call a function which uses these KeyPairs. 当前,我调用使用这些KeyPair的函数。
pressKeyPairs( gTestMenu );
pressKeyPairs( gBrowseMenu );
and more calls for the rest...
I would like to put all these KeyPair instantiations in a vector so I wouldn't have to call pressKeyPairs() a hundred times... I'm a newbie in using vectors... so I tried: 我想将所有这些KeyPair实例都放在向量中,这样我就不必一百次调用pressKeyPairs()了……我是使用向量的新手……所以我尝试了:
std::vector<KeyPair, std::allocator<KeyPair> > vMasterList;
vMasterList.push_back( *gTestMenu );
vMasterList.push_back( *gBrowseMenu );
std::vector<KeyPair, std::allocator<KeyPair> >::iterator iKeys;
for(iKeys = vMasterList.begin(); iKeys != vMasterList.end(); ++iKeys)
{
pressKeyPairs(*iKeys);
}
However, this code block isn't working... :( Can somebody tell me how to properly put these KeyPairs in a vector? 但是,此代码块无法正常工作... :(有人可以告诉我如何正确地将这些KeyPair放入向量中吗?
You've to use insert
to populate the vector with your different arrays. 您必须使用
insert
来使用不同的数组填充向量。 Here is how you should do it. 这是您应该如何做。
//initialized with one array
std::vector<KeyPair> vMasterList(gTestMenu, gTestMenu + 3);
//adding more items
vMasterList.insert( vMasterList.end(), gBrowseMenu , gBrowseMenu + 3);
And then reimplement your pressKeyPair
function, so that you can use std::for_each
from <algorithm>
header file as, 然后重新实现您的
pressKeyPair
函数,以便可以将<algorithm>
头文件中的std::for_each
用作,
//pressKeyPair will be called for each item in the list!
std::for_each(vMasterList.begin(), vMasterList.end(), pressKeyPair);
Here is how you can write the pressKeyPair
function: 这是编写
pressKeyPair
函数的方法:
void pressKeyPair(KeyPair &keyPair) //takes just one item!
{
//press key pair!
}
In my opinion, this is better design, as it doesn't need "manual" loop anymore at the calling site! 我认为,这是更好的设计,因为它不再需要调用站点的“手动”循环!
You can even call pressKeyPair
for first 5 items in the list as, 您甚至可以为列表中的前5个项目调用
pressKeyPair
,例如,
//pressKeyPair will be called for first 5 items in the list!
std::for_each(vMasterList.begin(), vMasterList.begin() + 5, pressKeyPair);
One more example: 再举一个例子:
//pressKeyPair will be called for 5 items after the first 5 items, in the list!
std::for_each(vMasterList.begin()+5, vMasterList.begin() + 10, pressKeyPair);
EDIT: 编辑:
If you want to use manual loop, then you've to use this: 如果要使用手动循环,则必须使用以下方法:
std::vector<KeyPair>::iterator it;
for( it = vMasterList.begin(); it != vMasterList.end(); ++it)
{
pressKeyPair(*it);
}
But I would say it's not as elegant as the approach described earlier. 但是我要说的是,它不像前面描述的那样优雅。 Remember, this assumes that the function
pressKeyPair
has this signature: 请记住,这假设函数
pressKeyPair
具有以下签名:
void pressKeyPair(KeyPair &keyPair); //it doesn't accept array!
I think that the problem is that the code 我认为问题在于代码
vMasterList.push_back( *gTestMenu );
Only adds a single element of gTestMenu
to the vector, namely the first. 仅向向量添加
gTestMenu
的单个元素,即第一个。 The reason is that this code is equivalent to the following: 原因是此代码等效于以下代码:
vMasterList.push_back( gTestMenu[0] );
From which I think it's a bit easier to see what's going wrong. 我认为从那里看问题出处要容易一些。
To fix this, you probably want to add all of the elements in gTestMenu
to the master list. 要解决此问题,您可能希望将
gTestMenu
中的所有元素添加到主列表中。 You can do this using the three-parameter vector::insert
function: 您可以使用三参数
vector::insert
函数执行此操作:
vMasterList.insert(v.begin(), // Insert at the beginning
gTestMenu, // From the start of gTestMenu...
gTestMenu + kNumTests); // ... to the end of the list
Here, you'll need to specify how many tests are in gTestMenu
as kNumTests
. 在这里,您需要将
gTestMenu
测试指定为kNumTests
。 You can do the same for gBrowseMenu
. 您可以对
gBrowseMenu
执行相同的gBrowseMenu
。
By the way, you don't need to specify the allocator type in the vector
declaration if you just want to use the default std::allocator
. 顺便说一句,如果您只想使用默认的
std::allocator
,则无需在vector
声明中指定分配器类型。 You can just write 你可以写
std::vector<KeyPair> vMasterList;
And you'll be totally fine. 而且您会完全没事的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.