return an empty vector c++
85,472
Just
return vector<string>();
Or use list initialization (since C++11)
return {};
Related videos on Youtube
Author by
user3369592
current getting my master degree in Computer Science.
Updated on July 09, 2022Comments
-
user3369592 almost 2 years
The requirement is that I need to search a vector to see if it contains the value passed in as the parameter. If the value exists in the vector, I return the vector. Else, I return an empty vector. I am not sure how to return an empty vector in c++. hope you could help me. my mimic.h:
vector<Pair> map;
my Pair.h:
Pair(){ } ~Pair(){} string prefix; vector<string> sufix;
Return vector function :
vector<string> Mimic::getSuffixList(string prefix){ int find=0; for(int i =0; i < map.size(); i++) { if(map[i].prefix == prefix) { find =1; return map[i].sufix; //sufix is a vector from a class called "Pair.h" } } if(find==0) { //return an empty vector. } }
-
chris almost 10 years
{}
works well. -
excavator over 5 yearshow to return an empty vector if the return type of the function is vector<T>& ?
-