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};
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.
01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
Good things donīt come easy in life!!!
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); }
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.