10 Replies - 3627 Views - Last Post: 02 November 2011 - 10:03 AM Rate Topic: -----

#1 ghostonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 11

declaring vector values

Posted 02 November 2011 - 06:59 AM

Hi i just wanted to know how to store values in vector eg i want to store values 10 and 20 into a vector called storage
then i want to reset the first element and store the value 4

vector <int> Storage(2) //This is to declare that my vector will have 2 values (c++)

vectorStorage.resize(4) // this will reset the 1 value and set it as 4 (c++)

can some one help me or explain how to store values in a vector and not generate random values... ?

im using c++

thx

Is This A Good Question/Topic? 0
  • +

Replies To: declaring vector values

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: declaring vector values

Posted 02 November 2011 - 07:07 AM

There are several ways to initialize a vector with values, you can use the constructor. Or you can assign values to each element using either array notation [] or using the at() member function.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 ghostonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 11

Re: declaring vector values

Posted 02 November 2011 - 07:25 AM

yes fai enuf but when i do do that like

vector <int> storage (1 , 2) that will copy 2 one time wont it ? (C++)
Was This Post Helpful? 0
  • +
  • -

#4 ghostonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 11

Re: declaring vector values

Posted 02 November 2011 - 07:31 AM

those tutorial you gave me are for arays i need vectors ....

i want to store values using vectors not arays
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: declaring vector values

Posted 02 November 2011 - 07:38 AM

Did you read the links? All of the links I posted are for std::vector.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 ghostonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 11

Re: declaring vector values

Posted 02 November 2011 - 08:09 AM

lol i dont know maybe its the education in this country but im not finding any help on vectors there ....

let me write the question down here it is

declare a vector storage to store objects of type int (do not specify the size of storage)

store the following values into storage : 10 28

reset the the 1st element of storage to 12

so im stuck arnt vectors suposed to be like vector <int> storage() //c++ the question sayes nothing about arrays....
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: declaring vector values

Posted 02 November 2011 - 08:19 AM

So how do you declare a vector of ints?

How do you insert elements into a vector?

How do you store values into a regular variable?

Show some actual code where you have tried to accomplish the objectives.

The farther you go in programming the more you will be required to be able to read and understand the documentation for the concepts you are trying to use. The links I provided are parts of the documentation for the std::vector class and have nothing to do with arrays.

Jim

This post has been edited by jimblumberg: 02 November 2011 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 ghostonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 11

Re: declaring vector values

Posted 02 November 2011 - 08:28 AM

i wrote it in my post

to declare to values in vector i said

vector <int> Storage(2) // this declares that the vector will have 2 objects (C++)
i dont know how to declare the values to that vector

vectorStorage.resize(12) // this will reset the 1st value to 12 (c++) and this will reset 1 value not 2

thats all im geting is this wrong ?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: declaring vector values

Posted 02 November 2011 - 08:38 AM

Quote

declare a vector storage to store objects of type int (do not specify the size of storage)

Does the following meet all the above requirements?
vector <int> Storage(2) // this declares that the vector will have 2 objects (C++)

No it does not. You are required to declare a vector storage but not specify the size. How do you declare a vector without specifying the size?

Your instructions mention nothing about "resizing" the vector, so why are you trying to use that construct?

Read this link: insert for how you insert elements into a vector.



Jim

This post has been edited by jimblumberg: 02 November 2011 - 08:38 AM

Was This Post Helpful? 1
  • +
  • -

#10 ghostonline  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 11

Re: declaring vector values

Posted 02 November 2011 - 09:51 AM

thx that info u gave me helped alot :)

that was more usefull
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4100
  • View blog
  • Posts: 12,695
  • Joined: 25-December 09

Re: declaring vector values

Posted 02 November 2011 - 10:03 AM

I'm glad it helped, but I gave you basically the same links in post #7. You really need to learn to read the documentation for the concepts you are trying to use.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1