|
-
February 8th, 2008, 12:20 AM
#1
Dynamic arrays
Hi!
Is it possible to give values to a dynamic array once you create it?
Code:
int * numbers = new int[10];
numbers[0] = 3;
numbers[1] = 6;
numbers[2] = 7;
numbers[3] = 8;
numbers[4] = 11;
numbers[5] = 12;
numbers[6] = 45;
numbers[7] = 77;
numbers[8] = 87;
numbers[9] = 99;
Come on, there must be an easier way to do this.
-
February 8th, 2008, 12:51 AM
#2
Re: Dynamic arrays
Is it possible to give values to a dynamic array once you create it?
Yes, and you have pretty much done it 
Okay, I suspect you are looking for a dynamic array version of array initialisation:
Code:
int numbers[10] = {3, 6, 7, 8, 11, 12, 45, 77, 87, 99};
I do not think it is possible, at least for now. If you really wanted the convenience of an initialiser list, you could initialise a temporary array and then use say, std::copy to copy over the values. With a std::vector, the constructor that takes two iterators could be used.
Speaking of std::vector, I hope you normally use that (or some other container) instead of a raw dynamically allocated array.
-
February 8th, 2008, 02:14 AM
#3
Re: Dynamic arrays
If using stl and boost:
Code:
#include <boost/assign/std/vector.hpp>
using namespace std;
using namespace boost::assign;
vector<int> numbers;
numbers += 3, 6, 7, 8, 11, 12, 45, 77, 87, 99;
-
February 8th, 2008, 07:47 AM
#4
Re: Dynamic arrays
Thank you both for answering!
I thought that it wouldn't be possible, and now i don't worry about it anymore.
Thank you laserlight for giving this temporary array idea. It would save lot of time. Good would be to use a function to copy the values of temporary array to
the new one, then after executing the function the temporary array would be deleted, right?
-
February 8th, 2008, 10:57 AM
#5
Re: Dynamic arrays
I would also add, that anytime you are using new and delete, you should ask yourself, do I need to manage memory manually? The more new and delete you have in your program, more potential for bugs.
-
February 8th, 2008, 11:08 AM
#6
Re: Dynamic arrays
Stroustrup and Reis made a proposal to the standardization committe for initializer lists, that do what you are trying to do. Thus with the next C++ standard, you will be able to more easily initialize other containers than arrays.
Read here for more info: http://www.open-std.org/jtc1/sc22/wg...2007/n2220.pdf
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
February 8th, 2008, 03:30 PM
#7
Re: Dynamic arrays
I would also add, that anytime you are using new and delete, you should ask yourself, do I need to manage memory manually? The more new and delete you have in your program, more potential for bugs.
Yeah, i know.
But i am not using these for particular program, initializing 10 integers to pointer really doesn't need memory managing.
I am still learning C++ and reading one book about it. There is an exercise where reader is asked to make a program that uses new to initialize an array of structures and give them values (pain in the ***).
I just hoped that there is an easier way than initializing them separately.
Now i know that in the moment there isn't, but thanks to treuss I also know that it may come.
Anyway, thanks everyone for helping me!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|