Greetings...

I am trying to get a 2-d vector filled with a records form a dataset.

this is what I have:

int StudentNum = 1, rflds;
TDataSet *Students = DSet;
String stu;
rflds = Students->FieldCount;  //Number of fields in the dataset
vector< vector<String> > dataset(Students->RecordCount, vector<String>());
while (StudentNum <= Students->RecordCount)
{
	Students->RecNo = StudentNum;
	for (int i = 0; i <= rflds-1; i++)
	{
		stu = Students->Fields->Fields[i]->AsString;
		dataset[StudentNum][i] = stu;
	}

StudentNum++;
}

I can fill a 1-D vector with no issue.

After I get that done, I will want to find the longest element for each of the vectors dataset[StudentNum], or the longest element in say dataset[StudentNum][2]. I assume you use dataset[StudentNum][2].size()...

If you can answer both that would be great

Thank you so much

Lee

What's a problem? You initialize inner vectors incorrectly. Furthemore, you must take a decision for a suitable vector elements indexation (StudentNum starts from 1, but the 1st index in a vector is equal to 0).

int StudentNum = 1, rflds;
TDataSet *Students = DSet;
String stu;
rflds = Students->FieldCount;  //Number of fields in the dataset
vector< vector<String> > dataset(Students->RecordCount, vector<String>());
while (StudentNum <= Students->RecordCount)
{
    Students->RecNo = StudentNum;
    for (int i = 0; i < rflds; i++)
    {
        stu = Students->Fields->Fields[i]->AsString;
        dataset[StudentNum-1].push_back(stu);
    }
    StudentNum++;
}

Now you have an initialized vector of vectors of String. Process it as you wish (in 2D array manner, for example);

for (size_t i = 0; i < dataset.size; ++i)
{
     for (size_t j = 0; dataset[i].size(); ++i)
     {
      ..... dataset[i][j] ...
     }
}

Resolved filling the 2d vector

{
int StudentNum = 1, rflds, fieldlength;
TDataSet *Students = DSet;
String stu;
rflds = Students->FieldCount;  //Number of fields in the dataset
vector< vector<String> > dataset(Students->RecordCount+1, vector<String>(rflds,0));
	while (StudentNum < Students->RecordCount)
	{
		Students->RecNo = StudentNum;
		for (int i = 0; i < rflds; i++)
		{
			stu = Students->Fields->Fields[i]->AsString;
			dataset[StudentNum][i] = stu;
		}
	StudentNum++;
	}
}

Now I just need to find the length of the elements...

{
int StudentNum = 1, rflds, fieldlength;
TDataSet *Students = DSet;
String stu;
rflds = Students->FieldCount;  //Number of fields in the dataset
vector< vector<String> > dataset(Students->RecordCount+1, vector<String>(rflds,0));
	while (StudentNum < Students->RecordCount)
	{
		Students->RecNo = StudentNum;
		for (int i = 0; i < rflds; i++)
		{
			stu = Students->Fields->Fields[i]->AsString;
			dataset[StudentNum][i] = stu;
		}
	StudentNum++;
	}
}

I think the issues were the less then or equal checks, and I had to define the length of each record<vector>.

My mistake, I posted the resolution twice, but that was not the resolution in total. the while state me should be

while (StudentNum <= Students->RecordCount)

With the <= I get all the records added to the vector. Now I just have to find the longest element in each of the fields per record... any ideas?

Thanks again

1. Honestly speaking, I don't understand what exactly you want to find: maximal length of the data in every column, or the max length of the field in the dataset, or a number of the record with longest data fileld (or what else).
2. God knows, how many String classes are in C++ world now (but there is one and only one std::string class, thank God!).

May be, you can post me/us what's your String class length member function (or property or what else;))?

Greetings... thank you for your response. what I am trying to do is find the longest value in a column of data so that when I tell the printer to move over for the vertical placement, I can do so dynamically rather then statically. Here is what I have so far:

int StudentNum = 1, rflds, fieldlength, vpage = 0, hpage = 375; //hpage where on the page it is supposed to start horizontally
String stu;
TDataSet *Students = Form1->DSet; //creates TDataSet *students from the ADOdataset 
rflds = Students->FieldCount;  //Number of fields in the dataset
vector< vector<String> > dataset(Students->RecordCount+1, vector<String>(rflds,0));
	while (StudentNum <= Students->RecordCount) // fills that vector with the dataset 
	{
		Students->RecNo = StudentNum;
		for (int i = 0; i < rflds; i++)
		{
			stu = Students->Fields->Fields[i]->AsString;
			dataset[StudentNum][i] = stu;
		}
		StudentNum++;
	}

	for(int FieldNum = 0; FieldNum < Students->FieldCount; FieldNum++)
	{
		for (int i = 1; i < dataset.size(); i++)
		{
			fieldlength = dataset[i][FieldNum].Length();
		}
	}
pr->Orientation = poLandscape; // just printing the first line of the dataset for now
pr->BeginDoc();
	for(int FieldNum = 0; FieldNum < Students->FieldCount; FieldNum++)
	{
		stu = dataset[1][FieldNum];
		pr->Canvas->TextOutA(hpage, vpage, stu);			//name
		hpage += 400;
	}
pr->EndDoc();

so that is why I need to know the max length of each element per column... so when the report prints it is not jagged, and looks nice and is consistent.

Well, it's so easy... You know a number of fields (rflds). Just before while loop write:

vector<int> maxlen(rflds,0);
int fldlen;

Now insert obvious statements in the inner loop:

while (...)
{
    ...
    for (int i = 0; ...
    {
         stu = ...
         fldlen = stu.Length();
         if (fldlen > maxlen[i])
            maxlen[i] = fldlen;
         ...
     }
}
// All done: dataset and maxlen vectors are ready to use...

You have maxlen vector where maxlen is equal to max length of the i-th field.

Honestly, I can't understand why it's so hard subtask for you. It's much more easy thing than the other ADO stuff in your program.

Apropos, why print loop header is so complicated? Why FieldNum < Students->FieldCount ? You know that it's rflds (see your source next lines above). Why StudentNum's 1st value is 1? Why the 1st dataset vector element dataset[0] is an empty one and real records are placed from the 2nd row?

It seems it's inaccurate, incostistent and error prone approach...

Well, it's so easy... You know a number of fields (rflds). Just before while loop write:

vector<int> maxlen(rflds,0);
int fldlen;

Now insert obvious statements in the inner loop:

while (...)
{
    ...
    for (int i = 0; ...
    {
         stu = ...
         fldlen = stu.Length();
         if (fldlen > maxlen[i])
            maxlen[i] = fldlen;
         ...
     }
}
// All done: dataset and maxlen vectors are ready to use...

You have maxlen vector where maxlen is equal to max length of the i-th field.

Honestly, I can't understand why it's so hard subtask for you. It's much more easy thing than the other ADO stuff in your program.

Apropos, why print loop header is so complicated? Why FieldNum < Students->FieldCount ? You know that it's rflds (see your source next lines above). Why StudentNum's 1st value is 1? Why the 1st dataset vector element dataset[0] is an empty one and real records are placed from the 2nd row?

It seems it's inaccurate, incostistent and error prone approach...

Doing it that way allows me to use the same code for different datasets. I have accommodated for the zero / 1 row.. Thank you for your input I will work with the maxlen tonight, and post the completed code.

Here is the final code that works. just fine. I needed to find the longest item per column. Once I find that I can set each column size appropriately. Thank you for your help.

{
int StudentNum = 1, rflds, fieldlength, vpage = 0, hpage = 375, maxLen, fldLen;
String stu;

TDataSet *Students = Form1->DSet;

vector< vector<String> > dataset(Students->RecordCount, vector<String>(rflds,0));

rflds = Students->FieldCount;  //maxnumber of fields in the dataset
dataset.empty();
	for (StudentNum = 1; StudentNum <= Students->RecordCount; StudentNum++)
	{
		Students->RecNo = StudentNum;
		for (int i = 0; i < rflds; i++)
		{
			stu = Students->Fields->Fields[i]->AsString;
			dataset[StudentNum-1][i] = stu;
		}
	}
	for (unsigned int col = 0; col <= dataset.size(); col++)
	{
		for (int row = 1; row <= rflds; row++)
		{
			fldLen = dataset.size();
			if (fldLen > maxLen)
			{
				maxLen = fldLen;
			}
		}
	}
}

Fills the vector per row and checks the max fields length per column. This also sets dataset[0][0] to have the first element 1 of the first record.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.