comp125 assignment 2 (1 Viewer)

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
i must say this is another wtf type of assignment

man, we are suppose to use vectors in this case..and i can't even get a rough sketch of the algorithm yet
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
if your class has global variables the ctor is place to initalize them, so set rows/cols to what you want

oh, you can init the vector too by reserving space if you know about how much youll need.
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
you need to allocate space in your vector before assigning anything to it. v.at() returns the value at a given index, you cant assign with it. to assign use the [] operator, so go

int
main(void)
{
vector v;

v.reserve(3);
v[2] = 3;

cout << v[2] << endl;

return(0);
}
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
hmm!

ive played around with this a bit an the compiler i was using was a bit out of date and wouldnt support .at()

the short answer to your 1st example is you need to allocate some space before you use it, either with .reserver() or .assign() (and you should probably use .assign(n, 1) which will create room for n items and give them a value of 1). however, i really dont know whats going on here, .at() seems only to work when you use v.assign() to allocate the space, even just for printing. looking at the definition for .assign() its

T& at(unsigned i);

so because it returns a reference (which you can tell from the & sign) its valid to use for assignment. i dont know why it doesnt work when you use .reserve(), but ill add it to my "why i think c++ is a shit language" list.

i actually have a language reference on c++ ill try dig up so i can give you a proper answer.
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
ok this is how it goes

you can give the vector an initial size in its ctor, eg

<pre>vector<int> v(10);</pre>

or, you can create space using <pre>v.resize()</pre> (which was undocumented in my paper reference, which that plus my old compiler is my lame excuse :p) or v.assign(). v.reserve() will reserve space but not allocate nor initialize it. you use it to say "my vector will probably reach around this size but not right now", vs v.resize() which says "please give me this much space now kthx". with v.reserve you still have to call v.resize() before you actually use the space.

the reason behind v.reserve() has to do with pointers. say you have a 10 element vector of items, and for whatever reason you have a pointer that points to one of the elements. if you later call v.resize(), the language may have to allocate more space in another region of memory and move everything there. if this happens the pointer you have will no longer point to the correct place. to counter this they created v.reserve(). with your 10 element vector if you called v.reserve(100), you could at any time call v.resize() with an argument upto 100 and its guarenteed by the language not to move things about in memory, so your pointer will still point to the right place.

if the above paragraph confuses you, dont worry, its not too important to understand.

hth
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top