2

I apologize if this question has been asked/answered elsewhere, but I didn't find anything since I'm not entirely sure what/how to ask this...

What I'm attempting to do is set up some kind of container; list, array, vector, what-ever, that will allow me to place and remove objects from specific indices.

Something like this:

[empty][empty][object][empty][object][object][empty]

I'm going to be moving objects from a vector into a specific index of this container and from this container to another vector.

What would be the best way to represent this and what kind of container would be best suited? I was originally using a vector, but the built in functions didn't seem to give me enough control over where the object ended up. It was either the front or the back.

I need to figure out the best way to hold those "empty" indices and move objects in and out of each element freely.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Prototype958
  • 179
  • 8
  • 2
    How about a std::map? – Vaughn Cato Feb 14 '14 at 21:30
  • 2
    You can place items wherever you want in a vector using the `[]` operator: `v[i] = item;` – Markku K. Feb 14 '14 at 21:30
  • 1
    @MarkkuK. You need to initialize enough slots first (e.g. with `resize()`). Because a vector cannot have holes, if you want empty slots you need a value that is interpreted as meaning empty (e.g. a null pointer, the integers 0 or -1, a user-defined object with some special state). –  Feb 14 '14 at 22:48
  • @delnan, agreed, that is good additional info for the OP. – Markku K. Feb 17 '14 at 16:59

2 Answers2

1

If I understand your question correctly you want to place data in your vector according to a certain pattern.

You can use a simple vector and implement functions yourself to place your data.

For example if you want to place data in every third place :

void placeItem(std::vector<int> my_vector, int element, unsigned int index){
    my_vector[((index+1)*3)-1]=element;
}

int retreiveItem(std::vector<int> my_vector, unsigned  int index){
    return my_vector[((index+1)*3)-1];
}

Then you can use placeItem and retreiveItem with indexes starting at 0.

If you simply meant that you want to place your data in arbitrary locations then you can use the [] syntax directly.

OlivierLi
  • 2,798
  • 1
  • 23
  • 30
1

A simple, definitely sub-optimal, but quite effective solution could be to use a vector in the following way:

#include <iostream>
#include <vector>
using namespace std;

struct Your_Object
{
    Your_Object& operator=(const Your_Object& other)
    {
        // Write a proper assignment operator here if you want to assign or swap values
        cout << "hello from assignment operator"<<endl;
        return *this;
    }
};

int main() {

    Your_Object nullObj;
    std::vector<Your_Object> vec;
    vec.reserve(10); // Creates 10 empty objects calling default constructors. Notice that this will NOT affect the vector's size, for that use resize()

    Your_Object space5, space3; // Two objects to put in space5 and space3

    // Put objects in space 5 and 3
    vec[5] = space5;
    vec[3] = space3;

    // Move object in space 5 to another place
    vec[1] = vec[5];

    return 0;
}

http://ideone.com/YDu6LC

If you manage to write a proper copy-assignment operator (perhaps with move semantics if you're using C++11) which does a deep-copy of your object and if of course this proves to be not too burdensome for you, the above might be a simple working system for what you need to do.

Just remember to resize (or reserve, for the difference take a look here: https://stackoverflow.com/a/7397862/1938163) the space you need in advance.

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • Thank you, David. I actually did not know about reserve and I think that is exactly what I am looking for. Also I feel very "noobish" not realizing vector elements could be accessed directly through the brackets... Probably over thinking that one and just never tried it I guess. This should be a huge help to my project. – Prototype958 Feb 18 '14 at 19:44