Thread: Inserting New Element to a vector List

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    1

    Question Inserting New Element to a vector List

    I am not understanding how Insert works...

    I am trying to insert a new element into my vector list as:

    Code:
    int n = 1;
    std::vector<Player_Class>::iterator it;
    it = PlayerList.begin();
    PlayerList.insert(it+n, new Player_Class());

    and it is giving me an error saying that the insert function is not overloaded to take the variables..

    How would I insert new Player_Class() to the PlayerList vector at a specified position?


    Ok, I think I miss understood how the vectors worked and they are not pointers to objects but that actual objects themselves. I am sure I understand now that if the vector was

    Code:
    std::Vector<Player_Class*> PlayerList
    std::vector<Player_Class*>::iterator it;
    Then it would work.

    I just removed new and I am not even using this anymore because I realized that it's position was unnecessary anyway for what I need. I am just using PlayerList.push_back()
    Last edited by AC_Player; 06-02-2018 at 09:06 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yep, sounds like you solved your own problem once you understood that the vector stores objects of the template type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting element in binary tree using pointers
    By Ajay.Kumar in forum C Programming
    Replies: 2
    Last Post: 09-08-2014, 06:36 PM
  2. vector<vector<int> > access element.
    By nimitzhunter in forum C++ Programming
    Replies: 0
    Last Post: 01-23-2011, 05:14 AM
  3. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  4. Need some help with inserting an element into a tree
    By winggx in forum C Programming
    Replies: 3
    Last Post: 03-06-2010, 10:46 PM
  5. inserting an element into an array
    By galmca in forum C Programming
    Replies: 14
    Last Post: 01-09-2005, 06:34 AM

Tags for this Thread