Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Vector of Vector
Vector of Vector [message #5955] Wed, 25 October 2006 22:09 Go to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
Hi,

I needed to substitute some structure build in C with pointers and I've recognised that it is equivalent to a container of Vector of Vector of the beautifull NTL library (U++ Core). I have not seen any example on this specific structure on the tutorials, so for me was not evident the syntax. After many trials ed errors the following code run with success. It populated the container, print it and produce the sort of a vector. What is very nice, for me, is that the structure became a matrix the which element can be accessed by index without stupid iterators. Marvelous!
I do not know if there are other more direct way to achieve the same purpose. If yes I'm interested in it. Perhaps this topic can be matter of a new tutorial on the NTL that merit to be known more in deep.

Luigi

#include <Core/Core.h>

CONSOLE_APP_MAIN
{  FileOut out( "C:\\upp\\out\\MSC8\\out.txt"); //set your dir and file name

    Vector<  Vector<int>  > array;  // vector of vector
    Vector<int> vec;

	//create a vector and add it to the array
	vec.Add(80);
	vec.Add(20);
	vec.Add(30);
	vec.Add(50);
	vec.Add(90);
    array.Add(vec);
    
	//create another vector and add it to the array
        vec.Clear();
	vec.Add(100);
	vec.Add(200);
	vec.Add(300);
    array.Add(vec);
    
	//create a vector and add it to the array
        vec.Clear();
	vec.Add(1000);
	vec.Add(2000);
	vec.Add(3000);
	vec.Add(3000);
    array.Add(vec);

    for(int i = 0; i < array.GetCount(); i++)
	  {
	    for(int j = 0; j < array[i].GetCount(); j++) out.Put( AsString(array[i][j]) + ", ");
	    out.Put("\n");
	  }
	    out.Put("\n\n\n");
	  
	// sort the first vector of the array
	Sort(array[0]);
    for(int i = 0; i < array.GetCount(); i++)
	  {
	    for(int j = 0; j < array[i].GetCount(); j++) out.Put( AsString(array[i][j]) + ", ");
	    out.Put("\n");
	  }
	  
	out.Close();
}
Re: Vector of Vector [message #5964 is a reply to message #5955] Thu, 26 October 2006 01:35 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
This is not very effective way to do it (in fact, it is as slow as STL Smile. Much better is to create it "in-place":

Vector< Vector<int> > x;
Vector<int>& n = x.Add();
n.Add(1);
....


Alternatively, you can use AddPick - that one would destroy source Vector avoiding the copy.

Mirek
Re: Vector of Vector [message #5965 is a reply to message #5964] Thu, 26 October 2006 01:36 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
P.S.: Investigate "LOG" and "DUMP"..
Re: Vector of Vector [message #5979 is a reply to message #5965] Thu, 26 October 2006 09:46 Go to previous message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
luzr wrote on Thu, 26 October 2006 01:36

P.S.: Investigate "LOG" and "DUMP"..


If LOG() is intented to print on a file in the directory of the application, then does not work with me.
I'll try DUMP.
Thanks for the improvement of the code... but I need some time to understand this syntax Smile .

Luigi
Previous Topic: Tip: Using FileSel
Next Topic: Groovey
Goto Forum:
  


Current Time: Fri Apr 19 18:21:27 CEST 2024

Total time taken to generate the page: 0.01731 seconds