This code snippet receives string, delimiter(space) and vector as argument and splits the string according to delimiter and stores it in vector. It is not storing anything into vector if i use push_back but works if i use [] operator. Can someone explain why push_back is not working?
void split(const string & input,char delim,vector<string> & elems){
stringstream ss;
ss.str(input);
string item;
int i = 0;
while(getline(ss,item,delim)){
//elems.push_back(item);
elems[i] = item;
i++;
}
}
int main(){
char delim = ' ';
vector<string> item(2);
string input;
getline(cin,input);
split(input,delim,item);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…