Return an Empty Vector C++ [Duplicate]

Return an Empty Vector C++ [Duplicate]

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. 
   }

  }
2

1 Answer

Just

return vector<string>();

Or use list initialization (since C++11)

return {};
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.