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};
Printable View
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};
Donīt think so, check in this for more info.
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.
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]);
}
How 'bout this jlou?
Code:std::vector<int> A;
{
int arr[] = { 1, 2, 4, 5, 7 };
A.assign(arr, arr+5);
}
You can't use thesyntax directly since that is made at compile time, and the compiler doesn't know of the vecto's internal representation.Code:= {1, 2, 3};
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 :(