返回一个空向量 c++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24175507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:39:50  来源:igfitidea点击:

return an empty vector c++

c++objectvectorreturn

提问by user3369592

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:

要求是我需要搜索一个向量以查看它是否包含作为参数传入的值。如果该值存在于向量中,则返回该向量。否则,我返回一个空向量。我不确定如何在 C++ 中返回一个空向量。希望你能帮助我。我的模仿者.h:

vector<Pair> map;

my Pair.h:

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

  }

回答by songyuanyao

Just

只是

return vector<string>();

Or use list initialization(since C++11)

或使用列表初始化(C++11 起)

return {};