The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 04-15-2008, 06:59 AM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
Inserting data at a certain point

I have a list of data stored in an array eg;

[0].x = 5
[0].y = 2
[0].z = 6

[1].x = 7
[1].y = 2
[1].z = 9

and I need to be able to be able to insert something, for example, between [0] and [1] without overwriting 1. I thought about using a vector with my class but I don't know much about vectors. The only other way I can think of doing it is to copy all the data from the array below to the array above, but this might take some time when I get alot of things stored in the array.

So, what would be the best method to insert something in between [0] and [1] easily?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 04-15-2008, 05:42 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 116
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
A vector is essentially a C++ array, being that the underlying structure is guaranteed to be contiguous ("raw" arrays are generally frowned upon in C++, because a vector does the same job with added safety).
insertion into the middle of a contiguous container such as an array or vector is a slow operation, done by 'shuffling' elements around (ie, copying).
a linked list does this much faster, by simply changing a link, however, you lose the luxury of random access with a linked list. There's no 'best' answer to what kind of container you should use.

With a list or vector, you can use iterators to hold the position where you'd like to insert something. As far as writing the code is concerned, this is a simple and clean way to add something into the middle of a container. vectors and lists have a method called insert() which takes an iterator parameter. (An iterator is a place-holder; similar to a pointer)

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
MrPickle (04-15-2008)
  #3 (permalink)  
Old 04-15-2008, 06:17 PM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
So what I'd essentially be doing is saving the positions I want?

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 06:15 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 116
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Not really - positions of objects in a container are an implementation detail specific to the container, so they're mostly irrelevant when it comes to inserting something. you'll probably never know the exact position that the element will end up, since positions change. You're more likely to know the information about the elements which should come before it and/or after it.

Take this as an (imperfect) analogy; If a tutor hands out text books to his/her students, the tutor may ask the students to read some page from the book. Just suppose that the tutor had a different edition of the book to all the students, and some students also had different editions of the book to each another. if the tutor simply said "Read page 61", implying the 61st page offset from the beginning of the book, it would be highly likely that all the students would go away, read the page, and come back all having read something completely different to what was on Page 61 of the tutor's book; despite being the same page number, the content between editions varies by a few pages, making the page numbers meaningless. The reliable way to communicate the right page, would have been to give a chapter heading, or section heading instead, so that the students could find the page, regardless of its position in the book.

Iterators are the same - they only work if you know details about the actual element which is currently at the position where you'd like to insert new data. you can't insert an element based on its "position", so instead you use a function to produce an iterator, based on some criteria. (especially since some containers, such as a linked list or binary tree, have no concept of ordinal positioning)

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 04-16-2008 at 06:18 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
MrPickle (04-17-2008)
  #5 (permalink)  
Old 04-18-2008, 11:19 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 317
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
mmm, iterators are my favorite - straight up to "inject" an element in an array and a pre-built method doesn't already exist in the array/vector/collection ect... You could re-assign the size of the array + 1, iterate through the array backwards until you reached the idx you need to insert into, then manually set that idx to Whatever you want. That's how I'd do it anways
Reply With Quote
  #6 (permalink)  
Old 04-20-2008, 03:23 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,122
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Hmm.. I guess you need to shift all of the values from the original position to the nextPosition in the array, this usually requires a tempArray that stores your oldValues, then change the size of your array, then store the tempArray back.. I guess that's easy enough..?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #7 (permalink)  
Old 04-22-2008, 02:00 PM
MrPickle's Avatar
MrPickle MrPickle is offline
PT Admin
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 332
iTrader: (0)
MrPickle is a jewel in the roughMrPickle is a jewel in the roughMrPickle is a jewel in the rough
Ok, well I decided to go with the insert function in the vector but I'm not really sure how to use it. I'm not sure how you give it your data.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 07:49 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50