-
dynamic array of type
Code:
Private Type tData
Label As String
Data As String
End Type
Private Sub Form_Load()
Dim arrData() As tData
End Sub
I want to do the same as above in c++ (i.e. a dynamic array that holds a struct). I realise I'll be using a struct and vector but I cannot work out the implementation.
Please can somebody give me a few ideas...
Many thanks
-
struct
{
int *someInts;
char *someChars;
long count;
}myStruct;
.....
// Some code
myStruct *theStruct = new myStruct[872893];
// Some more code
This is for dynamic arrays.
For vectors I think:
typedef vector<myStruct> myVector;
myVector haha;
haha.push_back(...)
And so on. It's the struct and typedef that matter. Declare the struct - or class - or whatever - then declare the vector using a typedef:
typedef vector<Type> VectorTypeName;
There you go. If my stuff is bad programming, don't worry as structs and vectors are detailed in the MSDN library.
OK?
HD
-
For Label and Data, I'd use the string class rather than a char* pointer.
-
Thanks for the replies.
I am still having trouble though:
I have the following class:
Code:
#ifndef __CCONFIG_H__
#define __CCONFIG_H__
#include "CFile.h"
#include <vector>
class CConfig
{
public:
int Parse(const std::string &path);
struct CONFIG {std::string Label; std::string Value;};
private:
std::vector<CONFIG> config();
};
#endif //__CCONFIG_H__
I need to know how I can access this from the class implmenatation to add elements to the vector.
Something like this? this->config[1].Label = "Label1";
many thanks...
-
I think that this:
std::vector<CONFIG> config();
needs to be changed to this:
typedef std::vector<CONFIG> config;
This defines that a vector containing elements of type CONFIG exists and has type config.
You have to initialise this:
config myConfigVector;
Then to add items you can use:
CONFIG thisConfig;
myConfigVector.push_back(thisConfig);
All this information on Vectors can be found in MSDN.
HD
-
many thanks guys. works like a dream.
don't need any typedefs though...
-
No problem. I'm so used to typedefs from C that I automatically do them.
HD
-
You don't need the typedefs, but they shorten your code and make it more readable.