Insert things in the beginning of a vector

Hello. I am having trouble finding a good tutorial about how to insert a number in the beginning of a vector. Some say that I should use push_back(), however I it seems as the push_back function does something else. Like push it back to the back of the vector. So how to I do to insert a number in the first part of the vector?
push_back() adds an element to the end. insert() should do the trick
http://www.cplusplus.com/reference/stl/vector/insert/
And heres all the functions of vector
http://www.cplusplus.com/reference/stl/vector/
Yeah I saw that one..However I am a herpaderp and I dont understand everything. I just need a plain example how to insert a normal number which the user put into a variable. :S

Yeah..I am derp.
Haha you're not a derp! STL containers can be tricky. Just try not to over think them.

1
2
updateVec(int idx, int value) {
     myVec.insert(myVec.begin() + idx, value);


Here's a pretty simple example. idx refers to the index, and value is the value you are inserting.
myVec.begin() as you could guess, is the first element (or 0) of the vector. idx is really just an int, but it works as an index because it will move for x spaces in the vector, getting to that index.

So, if you do myVec.insert(myVec.begin() + 4, "ABC"), this will insert "ABC" to the begin() + 4 index, and push all preceding elements forward 1. It will also reallocate memory if need be

EDIT: Forgot to warn you, you need to be sure begin() + idx will try to access unallocated memory.
Last edited on
This will help: http://cplusplus.com/reference/stl/vector/insert/

1
2
3
4
5
6
7
vector<int> MyVector;

for (int i = 0; i < 10; i++)
    MyVector.insert(MyVector.begin(),i); // Inserts the value i at the start of the array 10 times.

for (vector<int>::iterator it=MyVector.begin(); it<MyVector.end(); it++)
    cout << *it; //Outputs the values of MyVector in order 


This will output
9876543210
Last edited on
If you need to add elements to the beginning of a vector you may want to consider using a deque instead:

http://cplusplus.com/reference/stl/deque/

It has a method to push_front() as well as push_back().
Last edited on
Thanks for all your help. Hopefully I this will help. If not ill post back. Thanks! :)
@Framework.

You can't just say "calling insert". It does not give me a clue on what to do. If I call insert the function obviously requires several parameters inside the parentheses. This forum is to help and give advice, you are doing neither.

I don't know who reported your post but I damn sure think that he or she who did it was right.
Shooninjo ignore him, someone hacked his account.
I assume that he is the one reporting himself?
Using insert with a vector or deque is almost always a poor choice. To insert into a vector, every element needs to be moved forward to make space (meaning this process is also slowed down the more elements you insert). Insertion is most commonly done with (and should almost exclusively be done with) the list container because the elements aren't stored contiguously in memory. This means you can just have the element before where you are inserting point to the new element, and have your new node point to the element the node pointing to the new element points too. This makes insertion into lists very fast, but also has a consequence, random access of elements is very slow. Because elements aren't contiguously stored in memory you can't use pointer arithmetic to access elements. Instead you have to traverse through the entire list until you find the needed element. This means that lists are most commonly used when you need to insert/remove elements quickly, and you intend to iterate through the elements of the list frequently. Vectors and Deques on the other hand, are excellent when you need to randomly access elements, but slow when you need to insert elements into them.

The hardest part of using the STL is often determining which container to use. Consider what you're using your container for, and the operations you will need to perform on it, and decide which you operation will be performed the most, and if there is no way to get around inserting or random access.

In your case I would recommend using a Deque, but I don't know all of the details of your problem.
Last edited on
I kinda need to story the places of letters in a sentence. So I though that I would use a vector for it
So ive made a Deque. This is how it looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include<deque>

using namespace std;

int main()
{
    int lol;
    cin>>lol;
    deque<int> my_deque(1,1);
    my_deque.push_front(lol);

    for(int i=0; i<3; i++){
    cout<<"\n\n"<<my_deque.at(i)<<endl;
    }




    return 0;
}


However is it possible to make an empty deque? For example without any parentheses, or empty parentheses, like my_deque(); However this does not work.
deque<int> my_deque;
Yeah...just read the reference... was to lazy to open a tab and search for it before lol. t.t
Topic archived. No new replies allowed.