Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I do a Dynamic Array like CByteArray for a STRUCT array ?

Status
Not open for further replies.

youssef

Programmer
Mar 13, 2001
96
BE
Hi,

I create a STRUCT. My STRUCT is composed only by BYTE datas.
I would like to know how can I do for use this STRUCT in a CByteArray or other.
For example : CByteArray.RemoveAll; CByteArray.Add; ...

If anyone can help me.

Best Regards

youssef
 
If you want to store object/structs of your own, try the CArray template class. Or you can store pointers to your items in CPtrArray. Or derive from CObject and use CObArray. Or derive a CTypePtrArray class. I suggest you read the MSDN sections about collection classes (there's a lot of them). Personally though I'd use the STL classes. Here's how you can do it in STL:

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <string>

using namespace std;

typedef string TKey;


struct TRecord
{
TKey key;
int data;
bool operator== (const TRecord& tdr) const {return key == tdr.key;}
bool operator< (const TRecord& tdr) const {return key < tdr.key;}
};

typedef vector<TRecord> tvtd;

// vector write -> stdout
void output(tvtd vstruct)
{
for(tvtd::iterator il = vstruct.begin(); il!=vstruct.end(); il++) cout << (*il).key << endl;
cout << &quot;******************&quot; << endl;
}

int main(int argc, char* argv[])
{
tvtd vstruct;
TRecord td;
tvtd::iterator il, il1;

td.key = &quot;Hi there13&quot;; // Insert at end
vstruct.push_back(td);

td.key = &quot;Hi there17&quot;; // Insert at end
vstruct.push_back(td);


td.key = &quot;Hi there04&quot;;
il1 = vstruct.insert(vstruct.end(),td); // Insert at end (save pointer)

td.key = &quot;Hi there22&quot;;
vstruct.insert(vstruct.begin(),td); // Insert at start

// vector write -> stdout
output(vstruct);

// Find
il = find(vstruct.begin(),vstruct.end(),td); // Find &quot;Hi there22&quot;
cout << (*il).key << endl; // Check correct Element
cout << &quot;******************&quot; << endl;

vstruct.erase(il); // &quot;Hi there22&quot; delete
vstruct.erase(il1); // &quot;Hi there04&quot; delete

// vector write -> stdout
output(vstruct);

// Noch einige Elemente hinzufügen
td.key = &quot;Hi there14&quot;; // Insert at end
vstruct.push_back(td);

td.key = &quot;Hi there05&quot;;
vstruct.insert(vstruct.end(),td); // Insert at end

td.key = &quot;Hi there19&quot;;
vstruct.insert(vstruct.begin(),td); // Insert at start

// vector write -> stdout
output(vstruct);

// vector sortieren
sort(vstruct.begin(),vstruct.end());

// vector write -> stdout
output(vstruct);

return 0;
}

This creates a vector of CRecords and does some stuff with it.

The operator overloads in the record struct are needed for operations like find and sort. If you don't need these, you don't need the operators. If you need MFC serialisation, you could add that pretty easily too, I would guess. :) I just can't help it, I like MS...:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top