🔒 Locked
Hard Coding Vectors question!
Advertisement
Original Post
Does anyone know if theres a way to hardcode vectors so that i can define all 20 elements of it in one line. For example to hardcode an array of integers you could do something like a[4] = {6,2,9,8,4}; any ideas?
sorry about that c++ vector
I believe this is something that is being talked about for inclusion in the next c++ standard. So, with luck, it may be widely available 10 years from now [grin].
Right now there isn't much better than:
Right now there isn't much better than:
Type array[] = { one, two , three , ... };std::vector<Type> vec( array, array + ( sizeof( array ) / sizeof( array[0] ) ) );
There's no real reason to hardcode a vector. I'm not making excuses to make up for C++'s lack of a way to do vector literals either :P
Just use an array. If you know the size already, why do you need a vector? A vector doesn't do much more than a vector (resizing being the key one). If you access your members through operator[], it doesn't do bounds checking either. Do you really need a vector? Are more elements going to be added to this collection?
rip-off's solution should work, but it's a little messy. Be careful with pointer arithmetic. Just make sure to make that array in a static scope. Either a global (or hidden in a namespace), a static in a function, or a static member variable. Make it const too!
Just use an array. If you know the size already, why do you need a vector? A vector doesn't do much more than a vector (resizing being the key one). If you access your members through operator[], it doesn't do bounds checking either. Do you really need a vector? Are more elements going to be added to this collection?
rip-off's solution should work, but it's a little messy. Be careful with pointer arithmetic. Just make sure to make that array in a static scope. Either a global (or hidden in a namespace), a static in a function, or a static member variable. Make it const too!
Quote:
Original post by jonahrowley
There's no real reason to hardcode a vector. I'm not making excuses to make up for C++'s lack of a way to do vector literals either :P
Just use an array. If you know the size already, why do you need a vector? A vector doesn't do much more than a vector (resizing being the key one). If you access your members through operator[], it doesn't do bounds checking either. Do you really need a vector? Are more elements going to be added to this collection?
rip-off's solution should work, but it's a little messy. Be careful with pointer arithmetic. Just make sure to make that array in a static scope. Either a global (or hidden in a namespace), a static in a function, or a static member variable. Make it const too!
The vector will copy the contents of the array into itself, the array doesn't have to stick around.
A better option would be to have the data in a file, and load it into the vector. This can be quite easy, and is a much better alternative, given that you can change the data and not have to recompile your program.
And @jonahrowley:
Quote:
Do you really need a vector?
Do you lose anything by not using a vector? [grin]
typedef char blag[10];std::vector<blag> myvector;char hey[3]={'h','e','y'};for( int i = 0; i < 10; i++) myvector.push_back(hey);To fit your object type, though. This isn't exactly what you're talking about (unless it is) but you get the idea.
Quote:
Original post by rip-off
The vector will copy the contents of the array into itself, the array doesn't have to stick around.
My concern was performance. If this vector gets large and is a stack variable, not only are you allocating and populating an array, but copying all elements into a vector. Of course, it doesn't matter if both the array and vector are static variables.
The vetor object may be on the stack, but it's contents will be on the heap.
Quote:
Original post by jflanglois
There's also the Boost.Assignment library.
jfl.
Yep. And if you want constant-sized arrays (there is good reason not to use a std::vector if you *don't* want the resize functionality: you improve documentation and express intent more clearly by saying "the container is supposed to have this fixed size because of X design reason") that still have object-like functionality/general niceness, look at Boost.Array.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement