|
-
Oct 24th, 2002, 10:20 AM
#1
Thread Starter
Lively Member
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
-
Oct 24th, 2002, 10:29 AM
#2
Addicted Member
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
-
Oct 24th, 2002, 12:25 PM
#3
Monday Morning Lunatic
For Label and Data, I'd use the string class rather than a char* pointer.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Oct 25th, 2002, 03:54 AM
#4
Thread Starter
Lively Member
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...
-
Oct 25th, 2002, 04:22 AM
#5
Addicted Member
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
-
Oct 25th, 2002, 09:20 AM
#6
Thread Starter
Lively Member
many thanks guys. works like a dream.
don't need any typedefs though...
-
Oct 25th, 2002, 09:29 AM
#7
Addicted Member
No problem. I'm so used to typedefs from C that I automatically do them.
HD
-
Oct 25th, 2002, 10:58 AM
#8
You don't need the typedefs, but they shorten your code and make it more readable.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|