Ok I have been taking a stab at understanding the Standard Template Library, particularly vectors. I spent the day tackling this from all directions. So before I move on, thinking I have complete mastery of the subject. I would like you to check it over. I have written this 5 different ways.
This is what I started with.
#include "DarkGDK.h"
#include <string>
#include <vector>
using namespace std;
struct score_t
{
string name;
int value;
} score;
vector<score_t> scores;
void DarkGDK ( void )
{
score.name = "Tommy";
score.value = 10;
scores.push_back(score);
score.name = "Brian";
score.value = 51;
scores.push_back(score);
dbPrint ( ( char* ) scores[0].name.c_str( ) );
dbPrint ( ( double ) scores[0].value );
dbPrint ( );
dbPrint ( ( char* ) scores[1].name.c_str( ) );
dbPrint ( ( double ) scores[1].value );
dbWaitKey ( );
return;
}
And this is my final work.
#include "DarkGDK.h"
#include <vector>
using namespace std;
struct score_t
{
char* name;
int value;
} score;
vector<score_t*> scores;
vector<score_t*>::iterator iter;
void DarkGDK ( void )
{
scores.push_back( new score_t ( score ) );
iter = scores.begin( );
(*iter)->name = "Tommy";
(*iter)->value = 10;
scores.push_back( new score_t ( score ) );
iter = scores.begin( ); ++iter;
(*iter)->name = "Brian";
(*iter)->value = 51;
for ( iter = scores.begin( ); iter != scores.end( ); ++iter )
{
dbPrint ( (*iter)->name );
dbPrint ( ( double ) (*iter)->value );
dbPrint ( );
}
dbWaitKey ( );
return;
}
Thank you.