[英]How to put initialized structs in a struct?
I have a struct : 我有一个结构:
typedef struct
{
int nNum;
string str;
}KeyPair;
Then I initialize my struct into something like this: 然后,我将结构初始化为如下形式:
KeyPair keys[] =
{
{0, "tester"},
{2, "yadah"},
{0, "tester"}
};
And yet, let's say a number of other initializations: 但是,让我们说一些其他的初始化:
KeyPair keysA[] =
{
{0, "tester"},
{2, "yadah"},
{0, "tester"}
};
KeyPair keysB[] =
{
{0, "testeras"},
{2, "yadahsdf"},
{3, "testerasss"}
};
KeyPair OtherkeysA[] =
{
{1, "tester"},
{2, "yadah"},
{3, "tester"}
};
and like 20 more of 'em. 还有20多首
Now, how do I create another struct and initialize it such that it contains these initiazed KeyPairs? 现在,如何创建另一个结构并对其进行初始化,使其包含这些已初始化的KeyPair?
The reason for this is because I will repetitively call a function whose parameters would come for these structs. 这样做的原因是因为我将反复调用其参数将用于这些结构的函数。 And I DO NOT want to do it this way:
我不想这样做:
pressKeyPairs( keys, sizeof( keys) / sizeof( keys[0] ) );
pressKeyPairs( keysA, sizeof( keysA) / sizeof( keysA[0] ) );
pressKeyPairs( keysB, sizeof( keysB) / sizeof( keysB[0] ) );
pressKeyPairs( OtherkeysA, sizeof( OtherkeysA) / sizeof( OtherkeysA[0] ) );
and so on...
So I would like to just loop through a struct containing these inilialized instantiations of KeyPairs... 所以我只想遍历一个包含这些KeyPair的初始化实例的结构...
OR I would like to put these initialized instances of KeyPairs into a vector and just loop through the vector... How do I do that? 或者我想将这些KeyPairs的初始化实例放入向量中,然后循环遍历向量...我该怎么做?
Assuming that you have a fixed number key pairs, you could use a structure member function: 假设您有固定数量的密钥对,则可以使用结构成员函数:
typedef struct KeyPairs {
KeyPair keysA[3];
KeyPair keysB[3];
KeyPair otherKeysA[3];
void init() {
keysA[0].nNum = 0;
keysA[0].str = "tester";
keysA[1].nNum = 2;
keysA[1].str = "yadah";
keysA[2].nNum = 0;
keysA[2].str = "tester";
// and so on for other keys
}
} KeyPairs;
Then use it like so: 然后像这样使用它:
KeyPairs pairs;
pairs.init();
How about doing real C++ and using constructors ? 如何进行真正的C ++和使用构造函数?
(note that typedefs are implicits for structs in C++) (请注意,typedef是C ++中的隐式结构)
struct KeyPair
{
int nNum;
string str;
public:
KeyPair() {}
KeyPair(int n, string s) : nNum(n), str(s) {}
};
And then use another struct : 然后使用另一个结构:
struct TripleKeyPair
{
KeyPair keys[3];
TripleKeyPair()
{
// Your initialisation code goes here
}
};
And finally, I wouldn't advice using names such as : 最后,我不建议使用诸如以下名称:
KeysA, KeysB, KeysC ...
按键A,按键B,按键C ...
Arrays are exactly for this. 数组正是为此目的。 Why note use std::vector ?
为什么要注意使用std :: vector ?
How about using "null" objects as delimiters in the array? 如何将“空”对象用作数组中的分隔符? You would have to use constructors though:
但是,您将不得不使用构造函数:
struct KeyPair
{
KeyPair() : fIsEmpty(true) {}
KeyPair(int nNum_, const char *szStr) : nNum(nNum_), str(szStr), fIsEmpty(false) {}
int nNum;
string str;
bool fIsEmpty;
};
Then you can initialize it like this: 然后您可以像这样初始化它:
KeyPair allKeys[] =
{
KeyPair(0, "testeras"),
KeyPair(2, "yadahsdf"),
KeyPair(3, "testerasss"),
KeyPair(),
KeyPair(0, "tester"),
KeyPair(2, "yadah"),
KeyPair(3, "tester"),
KeyPair(1, "moreyadah"),
KeyPair()
};
And the iteration is trivial if you implement a kind of strlen() analog for KeyPair object array. 如果为KeyPair对象数组实现一种strlen()模拟,那么迭代就很简单了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.