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.