Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        3年前关闭。
                                                                                            
                
        
final_result会保存一系列递归函数调用的所有结果吗?我遇到段错误,但可以编译。
我很确定这不会起作用。但是,如果需要将所有结果存储在数据结构中,该怎么办?

vector< vector < pair < int,int> > > result;

vector< vector < pair < int,int> > > recursive_function()
{
  if(condition_met)  //never met on first call
  {
    result.push_back(a);  //a is a vector< pair < int,int> >
    return result;
  }
  else
  {
    return result;
  }
  //does something
  recursive_function();
  recursive_function();
}

vector< vector < pair < int,int> > > final_result;
final_result = recursive_function;

最佳答案

为什么不让生活变得更轻松,而只是将要累积结果的对象作为参考参数传递:

void recursive_function(vector<vector<pair<int, int>>> & result) {
    result.emplace_back();
    if (rand() % 2)
        recursive_function(result);
}

int main() {
    vector<vector<pair<int, int>>> result;
    recursive_function(result);
}

关于c++ - 如何在C++中定义,初始化和使用-vector <vector <pair <pair <int,int>,int>> v? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37233982/

10-12 05:27