# Vectors

• 02-26-2004
Korn1699
Vectors
Is there a way to declare and assign a set or values to a vector?

Like:
Code:

`vector<int> A = {1,2,4,5,7};`
• 02-26-2004
ripper079
• 02-26-2004
Korn1699
ok, I was just wondering, and I spent like 2 hours going though all the vector stuff I could find a while ago, and nobody I talked to knew. I was thinking that since it has many properties of arrays, that it might be able to be built that way.
• 02-26-2004
jlou
You could do something like this. There might be a better way to get the second input iterator. I added a dummy value to the end of the array so that I could pass that value as the end input iterator. I put it in a block and used assign, you could also just get rid of the block and put the array iterators into the constructor.
Code:

```std::vector<int> A; {     int arr[] = { 1, 2, 4, 5, 7, 0 };     A.assign(&arr[0], &arr[5]); }```
• 02-27-2004
CornedBee
How 'bout this jlou?
Code:

```std::vector<int> A; {     int arr[] = { 1, 2, 4, 5, 7 };     A.assign(arr, arr+5); }```
• 02-27-2004
Magos
You can't use the
Code:

`= {1, 2, 3};`
syntax directly since that is made at compile time, and the compiler doesn't know of the vecto's internal representation.

As pointed out you could create a dummy array and create your vector form it. The latter example is to recommend, since you don't need a dummy value at the end. Array pointers are allowed to point 1 element after the last element in an array.

FOOTNOTE:
Stupid forum, you aren't allowed to use curly brackets in normal text without using the CODE tags :(