Question

here is a code I have so far:

class Base
{
public:
    Base();
    void addClass(int index);
    vector<Class*> classList;
};

void Base::addClass(int index)
{
    if(classList.at(index) ) //want to check if there is nothing already store at
                             //that index
    {
        Class* c = new Class();
        //want to add this ^ to the classList vector at the index
    }
    //and so on

}

How would I go about doing this? It seems simple, but I'm still stuck. Any and all help with be greatly appreciated. Thank you!

Was it helpful?

Solution

A vector is sequential; there is something at every position between 0 and classList.size(), and nothing elsewhere. In the case of pointers (and some other things), the contents can be a null pointer, so while there is something, it is a very special something, which can be considered nothing at a higher level. If that's what you're looking for:

void
Base::addClass( int index )
{
    if ( classList.size() <= index ) {
        classList.resize( index + 1 );
    }
    if ( classList[index] == nullptr ) {
        classList[index] = new Class();
    }
}

First make sure that the vector is big enough, then test for the nullptr.

The vector will still contain entries for every index up to classList.size(). If you call addClass once, with an index of 1000000, that's going to mean a lot of empty entries. If this is your use case, then you should probably consider some sort of sparce data structure (perhaps a std::map<int, Class*). If the index aren't sparsely distributed, the vector solution is good.

And one last point: it's almost never appropriate to use std::vector<>::at(). If an out of bounds index is a programming error, you want the program to crash (which is the usual behavior in non-optimizing builds); what you don't want in case of a program error is to unwind the stack (in the usual case—there are exceptions).

OTHER TIPS

I would recommend using std::map<int, Class*>.

If you insist on using std::vector<Class*>, you have to call the resize() function to allocate some space in advance.

This would increase the classList size by one:

    Class* c = new Class();
    classList.insert(classList.begin() + index, c);

You can subscript into a vector:

classList[index] = c;

You could also use the insert function, although there's no easy way to pass a numeric index. You must add to an iterator:

classList.insert(classList.begin() + index, c);

You have to check the size of the vector. Otherwise if index is greater than size, program will crash because of accessing invalid memory location.

void Base::addClass(int index)
{
   Class* c = new Class();
  if(classList.size()< index) //check size
  {
      classList.resize(index+1); //resize if less then index
      classList[index]=c; // this location must be empty, as it was just allocated by previous line
  }
  else if(classList.at(index) == NULL) //else if that location is empty, place it                             
  {
     classList[index]=c;
  }
  else{ //else add at the end of the vector
     classList.push_back(c)
  }

}

Maybe something like this would suffice? this would replace the item at the index specified without using classList[i] (should use iterators with vectors).

Class* c = new Class();
classList.erase(classList.begin()+index);
classList.insert(classList.begin()+index, c);

working example: here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top