Two dimensional Vectors

5 posts / 0 new
Last post
turbo@bayour.com
turbo@bayour.com's picture
Offline
Mobile Conjurer
Joined: 25 Feb 2010
Posts:
Two dimensional Vectors

How is this done with MoSync? The 'Vector' don't seem to be the exact same implementation as 'vector'. Or I'm missing something...

I want the two dimensions to be 'the file counter' and 'the line counter'. The actuall data IN the vector should be a String...

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sam Pickard
rival's picture
Offline
Mobile Archmage
Joined: 19 Mar 2009
Posts:

Vectors are inherently one-dimensional. You probably want a two dimensional array like this

String myStrings[10][10];

See here for more http://www.fredosaurus.com/notes-cpp/arrayptr/22twodim.html

Niklas Nummelin
niklas's picture
Offline
Mobile Wizard
Joined: 18 Dec 2007
Posts:

Actually you can do a vector of vectors, to get the same effect.

Vector > > myStrings; (note that you need spaces between the '>' signs otherwise they will be parsed as the binary operator ">>" by the compiler).

// Niklas

turbo@bayour.com
turbo@bayour.com's picture
Offline
Mobile Conjurer
Joined: 25 Feb 2010
Posts:
"Niklas" wrote:

Actually you can do a vector of vectors, to get the same effect.

I tried that, but couldn't get it to work...

And to add a value to myStrings, you use:

myStrings[1].add("value1");

right?

Niklas Nummelin
niklas's picture
Offline
Mobile Wizard
Joined: 18 Dec 2007
Posts:

I think you haven't resized the outer vector. I did a little example. It outputs:
a b c
1 2 3

     Vector <Vector <String> > myString;
     myString.resize(2);
     myString[0].add("a");
     myString[0].add("b");
     myString[0].add("c");
     myString[1].add("1");
     myString[1].add("2");
     myString[1].add("3");

     for(int i = 0; i < myString.size(); i++) {
    	 for(int j = 0; j < myString[i].size(); j++) {
    		 printf("%s ", myString[i][j].c_str());
    	 }
    	 printf("\n");
     }

It's more memory / speed efficient to do a fixed sized array though if it can be (as Rival previously explained).

// Niklas