Wednesday, 15 February 2012

c++ - Finding information in a vector? -



c++ - Finding information in a vector? -

in c++ understand in order create dynamic array need utilize vectors. have problem when need find info set in vector.

for example: lets have simple vector stores name of person , little message wrote. in vector how find bill located.

i trying understand how in php when posted this question.

indeed seam confused. allow me seek help you.

one thing maybe confusing you: std::vector not geometric vector. it's sequence of info of same type contiguous in memory. it's array.

a) determine size of vector based on variable. illustration if using array array [x][y] ( know it's not possible this). how vector

std::vector automatically managed dynamic array.

it means array inside, it's managed code create sure array grows (gets bigger) when seek add together more info current capacity can hold.

actually, std::vector class template. means it's not real class, it's code compiler utilize generate real class. if

std::vector<int> my_ints; // vector of ints

this vector can hold ints. , then:

std::vector<std::string> name_list;

this 1 hold std::string objects.

as saying, inside, it's code manage array dynamically. can think previous examples if that:

class { unsigned long size; // count of elements contained in container unsigned long capacity; // count of elements memory allocated array can hold int* array; // array containing values, created using new, destroyed using delete } my_ints;

this oversimplified view of how inside, don't assume it's that, might useful.

now, when add together values, value copied in memory of array, in element not used yet (through push_back() example) or writing on element existing (using insert() example).

if add together value , capacity of vector not plenty hold values, the vector automatically grow: create much bigger array, re-create it's current values inside, re-create additional value too, delete array had before. it's of import understand this: if vector grows, can't assume it's info @ same adress in memory, pointers it's info can't trusted.

b) sec how using force command store value of variable within specific spot. 1 time again if using array it'd array[x][y] += q. x , y spot in array , q value.

you don't utilize push_back() add together value between 2 values, utilize insert().

the syntaxe array[x][y] += q not describe. add together q value @ position array[x][y].

c++

No comments:

Post a Comment