Hi. So my problem is that I can't save into a vector throughout a function, but I can save directly in the vector.

/*
 * asd.cpp
 * asd.cpp is licensed under GNU GENERAL PUBLIC LICENSE
 *  Created on: May 3, 2012
 *      Author: sin
 */
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void insert(int i, string elem, vector<string> st){
    st.insert(st.begin()+i, elem);
}
int main(){
    string cs;
    int i=0;
    vector<string> st (5);
    while (i<5){
        cout<<"\\: ";
        cin>>cs;
        insert(i, cs, st);
    i++;}
    for (int i=0;i<5;i++){
        cout<<st[i]<<" ";
    }
    cout<<"\\: ";
    cin>>cs;
    insert(0, cs, st);
    for (int i=0; i<5; i++){
        cout<<"\\: ";
        cin>>st[i];
    }
    for (int i=0;i<5;i++){
        cout<<st[i]<<"\n";
    }
    return (0);
}

So, my problem resides in the fact that when I try to add in the vector through the function insert, it won't add, but when I insert directly into the vector, it will save without any problems. Why is that?

insert(i, cs, st);

This code isn't working, and I was wondering why...
This is just a samll program that I've made only to see where the mistake is, but I'm working on a bigger project, with some classes and I'm working with this vector<type> standard library item and it won't save.

Recommended Answers

All 6 Replies

Because you have passed the vector into insert by value. When you pass by value a new copy of the variable (whatever type it is but vector in this case) is made for the lifetime of the function and gets deleted when the function exits.

So in your code when insert is called the vector is copied, a new value is inserted into the copy and then the copy is deleted leaving the original vector unchanged.

The fix is easy, don't pass by value pass by reference (note the &)

void insert(int i, string elem, vector<string>& st){
    st.insert(st.begin()+i, elem);
}

Well, thank you good sir, I totally forgot about that thing... Little things...

Your problem is in line 11.

void insert(int i, string elem, vector<string> st)

What you're doing here is a pass-by-value operation. This means that you're not actually passing your vector to the function, but rather just a copy of that vector. Your function modifies the copy, but the original vector remains untouched.

The fix is very simple --- turn it into a pass-by-reference operation by putting an ampersand after vector<string>.

void insert(int i, string elem, vector<string>& st)

But when I have a bigger project, for example a Movie class, which will store Movies by their id, title, description and type:

void Movie::setId(int i, int id){
    this->id.insert(this->id.begin()+i, id);
}

the int id will be added from futher classes, how can I insert without a copy of that? Shall I use the address of the classes, the & when I'm calling that method?

class Movie {
    friend class Repository::MovieRep;
    friend class Controller::MovieValidator;
    friend class Controller::MovieController;
private:
    int i, size, cp;
    vector <int> id;
    vector <string> title;
    vector <string> desc;
    vector <string> type;

For a better look at my class.

This is not gonna be a problem. When you call a member function of a class, it is called on the object pointed to by the "this" pointer. So, the this->id is the vector within that object, so it will be modified directly (without a copy).

No, I was asking about the fact that I have to pass the objects by reference, just as I did with the vector from the 1st program. But now it's ok, I've made it work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.