|
-
November 18th, 2010, 06:20 PM
#1
get an array of numbers from an input sequence of arbitrary length
Hello all,
How would you go about getting an array of numbers from an input sequence of arbitrary length? i.e. the user can input:
1 -2 3 -4 5 -6 7 -8
I understand dynamic memory allocation, and can think of a way to do this that would involve a lengthy algorithm of looping through and checking for whitespace/(- sign) on either side, but is there a better way to go about it?
thanks!
- iochi
-
November 18th, 2010, 06:23 PM
#2
Re: get an array of numbers from an input sequence of arbitrary length
Just use a vector with push_back ... std::vector takes care of
all the re-allocation.
Code:
vector<int> v;
int value;
while (get next number)
{
v.push_back(value);
}
You could use other containers also : std::list , std::deque
-
November 19th, 2010, 11:10 PM
#3
Re: get an array of numbers from an input sequence of arbitrary length
Ah I'm sorry, I forgot to mention that this has to be done in C. Will this approach still work? (or is there another way that I have to do it?)
Thanks!
-
November 19th, 2010, 11:30 PM
#4
Re: get an array of numbers from an input sequence of arbitrary length
sscanf? and allocate an array of 1 element. If sscanf("%d") succeeds then increase the size of the array with like realloc
01101000011001010110110001101100011011110010000001110011011001010111100001111001
-
November 19th, 2010, 11:30 PM
#5
Re: get an array of numbers from an input sequence of arbitrary length
sscanf? and allocate an array of 1 element. If sscanf("%d") succeeds then increase the size of the array with like realloc
01101000011001010110110001101100011011110010000001110011011001010111100001111001
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
|