| | |
Adding things to Array-Based Lists
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 31
Reputation:
Solved Threads: 0
I have a question about adding say integers to an array-based list of numbers.
I know how to add something at the end it would be something like:
values[count] = newValue;
count ++;
What about if someone wanted to add something to the beginning or somewhere between? My book doesn't say how to do such things because with an unsorted list order doesn't matter so it only thinks you would just throw things on the end. But what if you didn't want to?
I know how to add something at the end it would be something like:
values[count] = newValue;
count ++;
What about if someone wanted to add something to the beginning or somewhere between? My book doesn't say how to do such things because with an unsorted list order doesn't matter so it only thinks you would just throw things on the end. But what if you didn't want to?
>What about if someone wanted to add something to the beginning or somewhere between?
It's more or less the same deal. You move everything from the index you want to add to, to the end of the array forward by one element. Then you add the new value to the index you want and increase the count:
It's not desirable because it's potentially very expensive to shift array elements like that.
It's more or less the same deal. You move everything from the index you want to add to, to the end of the array forward by one element. Then you add the new value to the index you want and increase the count:
C++ Syntax (Toggle Plain Text)
for ( int i = count; i > index; i-- ) values[i] = values[i - 1]; values[index] = newValue; ++count;
Last edited by Narue; Oct 16th, 2007 at 6:29 pm.
I'm here to prove you wrong.
If you want to keep all existing values in the array then you need use a loop to shift everything down from the spot you want to add the new value.
[edit]^^^ what Narue said [/edit]
[edit]^^^ what Narue said [/edit]
Last edited by Ancient Dragon; Oct 16th, 2007 at 6:31 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- Problem on array based list (C)
- Adding Content to image based web pages? (Site Layout and Usability)
- Graphics In Pixel: Part II B (C)
- reading a file into code (Java)
- Error listing files... (C++)
Other Threads in the C++ Forum
- Previous Thread: hangman program stuck.. nid help.
- Next Thread: Help with Loop (C++/MFC)
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






