Gabriel L. P. Abreu
Gabriel L. P. Abreu

Reputation: 21

How to insert objects into std::vector?

I am finding trouble inserting an object into a std::vector, following the example of what I'm trying to do:

//in SomeClass.cpp

void SomeClass::addItem(int32_t &position, OtherClass &value)
{
    vectorOtherClass.insert(position,valvalue);
}

however I get the following error when trying to compile the program:

error: no matching function for call to ‘std::vector::insert(int32_t&, OtherClass&)’

vectorOtherClass.insert(position,value);

______________________^

the vector definition in SomeClass.h is:

private:
    std::vector<OtherClass> vectorOtherClass;

How can I properly insert an object into a vector in C ++?

And one last question, are the objects stored by reference or by copy within the vector?

Upvotes: 2

Views: 7074

Answers (3)

T33C
T33C

Reputation: 4429

Refer to: http://en.cppreference.com/w/cpp/container/vector/insert

You need to pass in an interator, so use begin and offset the position from there. No need to pass in ints by ref unless your function is going to change them. Consider checking for buffer overflow.

void SomeClass::addItem(int32_t position, const OtherClass &value)
{
    assert(position < vectorOtherClass.size());
    assert(position >= 0);
    vectorOtherClass.insert(vectorOtherClass.begin()+position, value);
}

Upvotes: 1

zebasz
zebasz

Reputation: 788

According to the method's reference, the insert method takes the following parameters:

position

Position in the vector where the new elements are inserted. iterator is a member type, defined as a random access iterator type that points to elements.

val

Value to be copied (or moved) to the inserted elements. Member type value_type is the type of the elements in the container, defined in deque as an alias of its first template parameter (T).

Note that position is not an integral value, but rather an iterator. C++ use iterators a lot, since many operations are fairly efficient when you have one.

In particular, you can add vector iterators and numbers to get the iterator to the correct position, so you can do something like:

vectorOtherClass.insert(vectorOtherClass.begin() + position, value);

This does not apply to other containers such as std::list. Also, you should make sure to check position is within the bounds of the vector (0 <= position < vectorOtherClass.size()). Ideally, position should be unsigned to ensure the lower bound.


Lastly, elements are added to std::vectors as copies. Vectors use an array internally, so values are copied into it. The array is resized (copied and replaced) as needed.

Upvotes: 1

jsn
jsn

Reputation: 81

Like the error says, there is no insert function with int parameter. See:

single element (1)  
iterator insert (iterator position, const value_type& val);
fill (2)    
    void insert (iterator position, size_type n, const value_type& val);
range (3)   
template <class InputIterator>
    void insert (iterator position, InputIterator first, InputIterator last);

You can find an example here http://www.cplusplus.com/reference/vector/vector/insert/

int main ()
{
  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);

  std::cout << "myvector contains:";
  for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Upvotes: 1

Related Questions