![]() |
|
|
|
| ||||||
|
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. |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |
| |||
| 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) |
| The Following User Says Thank You to Bench For This Useful Post: | ||
MrPickle (04-15-2008) | ||
| |||
| 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) Last edited by Bench : 04-16-2008 at 06:18 PM. |
| The Following User Says Thank You to Bench For This Useful Post: | ||
MrPickle (04-17-2008) | ||
| |||
| 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 ![]() __________________ Day Cares | Golf Courses | Disc Golf Courses | Campgrounds | Ice Rinks | Paintball Fields | Dentists | Plastic Surgeons | Aging Jokes Catholic Churches | Lutheran Churches | Methodist Churches | Episcopal Churches | Clean Jokes |
![]() |
| Thread Tools | |
| Display Modes | |
| |