return an empty vector c++

85,472

Just

return vector<string>();

Or use list initialization (since C++11)

return {};
Share:
85,472

Related videos on Youtube

user3369592
Author by

user3369592

current getting my master degree in Computer Science.

Updated on July 09, 2022

Comments

  • user3369592
    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
      chris almost 10 years
      {} works well.
    • excavator
      excavator over 5 years
      how to return an empty vector if the return type of the function is vector<T>& ?