English 中文(简体)
无法从矢量访问初始化结构的内容
原标题:Unable to access contents of initialized struct from a vector

我有一个结构:

typedef struct
{
    Qt::Key qKey;
    QString strFormType;
} KeyPair;

现在我初始化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....

目前,我调用了一个使用这些KeyPairs的函数。

pressKeyPairs( gTestMenu );
pressKeyPairs( gBrowseMenu );
and more calls for the rest... 

我想把所有这些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);
}

但是,此代码块不起作用…:(有人能告诉我如何将这些键对正确地放入向量中吗?)?

最佳回答

您必须使用insert来用不同的数组填充向量。以下是你应该怎么做。

//initialized with one array
std::vector<KeyPair> vMasterList(gTestMenu, gTestMenu + 3);

//adding more items
vMasterList.insert( vMasterList.end(),  gBrowseMenu , gBrowseMenu  + 3); 

然后重新实现pressKeyPair函数,这样您就可以使用<;算法>头文件为,

 //pressKeyPair will be called for each item in the list!
 std::for_each(vMasterList.begin(), vMasterList.end(), pressKeyPair);

以下是如何编写pressKeyPair函数:

  void pressKeyPair(KeyPair &keyPair) //takes just one item!
  {
       //press key pair!
  }

在我看来,这是一个更好的设计,因为它不再需要在调用站点上进行“手动”循环!

对于列表中的前5项,您甚至可以调用pressKeyPair

 //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);
}

但我想说,这并不像前面描述的方法那么优雅。请记住,这假设函数pressKeyPair具有以下签名:

void pressKeyPair(KeyPair &keyPair); //it doesn t accept array!
问题回答

我认为问题在于代码

vMasterList.push_back( *gTestMenu );

只将gTestMenu的单个元素添加到向量中,即第一个元素。原因是该代码等效于以下内容:

vMasterList.push_back( gTestMenu[0] );

我认为从中可以更容易地看出出了什么问题。

要解决此问题,您可能需要将gTestMenu中的所有元素添加到主列表中。您可以使用三个参数<code>vector::insert</code>函数来执行此操作:

vMasterList.insert(v.begin(),  // Insert at the beginning
                   gTestMenu,  // From the start of gTestMenu...
                   gTestMenu + kNumTests); // ... to the end of the list

在这里,您需要将gTestMenu中的测试数量指定为kNumTests。您可以对gBrowseMenu执行相同操作。

顺便说一句,如果您只想使用默认的std::allocater,则不需要在vector声明中指定分配器类型。你可以写

std::vector<KeyPair> vMasterList;

你会完全好起来的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签