C++ 无法从向量访问初始化结构的内容
我有一个结构:C++ 无法从向量访问初始化结构的内容,c++,qt,data-structures,vector,C++,Qt,Data Structures,Vector,我有一个结构: typedef struct { Qt::Key qKey; QString strFormType; } KeyPair; 现在我初始化密钥对实例化,以便将其用于我的自动测试应用程序 KeyPair gTestMenu[] = { { Qt::Key_1 , "MyForm" }, { Qt::Key_1 , "SubForm" }, { Qt::Key_Escape, "DesktopForm" } }; KeyPair gBrow
typedef struct
{
Qt::Key qKey;
QString strFormType;
} 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....
目前,我调用了一个使用这些键对的函数
pressKeyPairs( gTestMenu );
pressKeyPairs( gBrowseMenu );
and more calls for the rest...
我想把所有这些键对实例化放在一个向量中,这样我就不必调用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);
}
std::vector vMasterList;
vMasterList.向后推(*gTestMenu);
vMasterList.向后推送(*GBROWSEMEU);
std::vector::迭代器iKeys;
对于(iKeys=vMasterList.begin();iKeys!=vMasterList.end();++iKeys)
{
按键盘对(*i键);
}
但是,此代码块不起作用…:(有人能告诉我如何正确地将这些密钥对放入向量中吗?您必须使用
insert
使用不同的数组填充向量。以下是您应该如何操作
//initialized with one array
std::vector<KeyPair> vMasterList(gTestMenu, gTestMenu + 3);
//adding more items
vMasterList.insert( vMasterList.end(), gBrowseMenu , gBrowseMenu + 3);
以下是如何编写按键对
功能:
void pressKeyPair(KeyPair &keyPair) //takes just one item!
{
//press key pair!
}
vMasterList.insert(v.begin(), // Insert at the beginning
gTestMenu, // From the start of gTestMenu...
gTestMenu + kNumTests); // ... to the end of the list
在我看来,这是一个更好的设计,因为它不再需要调用站点的“手动”循环
您甚至可以对列表中的前5项调用按KeyPair
,如下所示:
//pressKeyPair will be called for first 5 items in the list!
std::for_each(vMasterList.begin(), vMasterList.begin() + 5, pressKeyPair);
还有一个例子:
//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);
编辑: 如果要使用手动循环,则必须使用以下选项:
std::vector<KeyPair>::iterator it;
for( it = vMasterList.begin(); it != vMasterList.end(); ++it)
{
pressKeyPair(*it);
}
您必须使用
insert
使用不同的数组填充向量。下面是您应该如何操作
//initialized with one array
std::vector<KeyPair> vMasterList(gTestMenu, gTestMenu + 3);
//adding more items
vMasterList.insert( vMasterList.end(), gBrowseMenu , gBrowseMenu + 3);
以下是如何编写按键对
功能:
void pressKeyPair(KeyPair &keyPair) //takes just one item!
{
//press key pair!
}
vMasterList.insert(v.begin(), // Insert at the beginning
gTestMenu, // From the start of gTestMenu...
gTestMenu + kNumTests); // ... to the end of the list
在我看来,这是一个更好的设计,因为它不再需要调用站点的“手动”循环
您甚至可以对列表中的前5项调用按KeyPair
,如下所示:
//pressKeyPair will be called for first 5 items in the list!
std::for_each(vMasterList.begin(), vMasterList.begin() + 5, pressKeyPair);
还有一个例子:
//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);
编辑: 如果要使用手动循环,则必须使用以下选项:
std::vector<KeyPair>::iterator it;
for( it = vMasterList.begin(); it != vMasterList.end(); ++it)
{
pressKeyPair(*it);
}
我认为问题在于代码
vMasterList.push_back( *gTestMenu );
仅向向量添加gTestMenu
的单个元素,即第一个元素。原因是此代码等效于以下内容:
vMasterList.push_back( gTestMenu[0] );
我认为从中更容易看出哪里出了问题
要解决此问题,您可能需要将gTestMenu
中的所有元素添加到主列表中。您可以使用三个参数vector::insert
函数:
void pressKeyPair(KeyPair &keyPair) //takes just one item!
{
//press key pair!
}
vMasterList.insert(v.begin(), // Insert at the beginning
gTestMenu, // From the start of gTestMenu...
gTestMenu + kNumTests); // ... to the end of the list
在这里,您需要将gTestMenu
中的测试数量指定为kNumTests
。您可以对gbrowsemeu
执行相同的操作
顺便说一下,如果您只想使用默认的std::allocator
,则不需要在vector
声明中指定分配器类型。您只需编写
std::vector<KeyPair> vMasterList;
std::vector vMasterList;
你会很好的。我认为问题在于代码
vMasterList.push_back( *gTestMenu );
仅向向量添加gTestMenu
的单个元素,即第一个元素。原因是此代码等效于以下内容:
vMasterList.push_back( gTestMenu[0] );
我认为从中更容易看出哪里出了问题
要解决此问题,您可能需要将gTestMenu
中的所有元素添加到主列表中。您可以使用三个参数vector::insert
函数:
void pressKeyPair(KeyPair &keyPair) //takes just one item!
{
//press key pair!
}
vMasterList.insert(v.begin(), // Insert at the beginning
gTestMenu, // From the start of gTestMenu...
gTestMenu + kNumTests); // ... to the end of the list
在这里,您需要将gTestMenu
中的测试数量指定为kNumTests
。您可以对gbrowsemeu
执行相同的操作
顺便说一下,如果您只想使用默认的std::allocator
,则不需要在vector
声明中指定分配器类型。您只需编写
std::vector<KeyPair> vMasterList;
std::vector vMasterList;
你会没事的。在std::vector vMasterList(gTestMenu,gTestMenu+3)中;3是从哪里来的?谢谢…)@Owen:这就是你初始化
gTestMenu
的方式<代码>GTEST菜单包含3项。我正在使用它。:-)谢谢纳瓦兹。。。如果我做一个手动循环呢?我该怎么做?我试过的循环无效…:(嗨,纳瓦兹,你说得对……手动循环对我不起作用。因此,我选择你优雅的方法。我实现pressKeyPair的方式是:模板无效pressKeyPair(KeyPair(&gKeys)[nNumOfElements]);因此我还可以得到密钥对的大小。但是,当我使用for_each进行此操作时,我会收到此错误消息……:(以下是std::vector vMasterList(gTestMenu,gTestMenu+3)中调用“for_each(u gnu cxx::u normal_迭代器,u gnu cxx:::u normal_迭代器)”的错误消息::错误2没有匹配函数;3来自哪里?谢谢……)@欧文:这就是你初始化gTestMenu
gTestMenu
包含3项的方法。我正在使用它。:-)谢谢纳瓦兹…如果我手动循环怎么办?我怎么做?我尝试的循环不起作用…:(嗨,纳瓦兹,你说得对……手动循环对我不起作用。因此,我选择你优雅的方法。我实现pressKeyPair的方式是:模板无效pressKeyPair(KeyPair(&gKeys)[nNumOfElements]);因此我还可以得到密钥对的大小。但是,当我使用for_each进行此操作时,我会收到此错误消息……:(以下是错误消息::错误2调用“for_each(u gnu_cxx::u normal_迭代器,u gnu cxx::u normal_迭代器,)”时没有匹配的函数)