2014-02-16 21 views
-1

所以我得到了这段代码。问题是用for循环创建空列表并添加1个整数。然后我将这个列表传递给DFS函数,它说列表是空的。任何想法为什么发生这种情况?传递给函数的非空列表变为空

#include <list> 
#include<vector> 
#include <iostream> 
using namespace std; 
list<short> integer; 
vector<list<short> > all; 

void DFS(list<short> ingeter, int N) 
{ 
    if(integer.empty()) 
    { 
     cout<<"IT IS EMPTY"<<endl; 
     return; 
    } 
    if(integer.size() > N || (integer.size() > 0 && (integer.front() == 0 || integer.back() % 2 == 0))) 
    { 
     return; 
    } 
    cout<<"size: "<<integer.size()<<endl; 
    all.push_back(integer); 
    for(short i = 0; i <= 9; ++i) 
    { 
     integer.push_back(i); 
     integer.push_front(i); 
     DFS(integer, N); 
     integer.pop_back(); 
     integer.pop_front(); 
    } 
} 
int main() 
{ 
    int N = 8; 
    for(short i = 0; i <= 9; ++i) 
    { 
     list<short> current; 
     current.push_back(i); 
     cout<<"size: "<<current.size()<<endl; 
     DFS(current, N); 
    } 
    return 0; 
} 
+0

请不要只是放下一堆代码,并要求我们阅读它。编译一个简短的例子(http://sscce.org/)来代替这个问题。 – filmor

+0

我缩短了它 – user1113314

+0

SSCCEE中的'c'意味着可编译。这段代码不会编译。 – filmor

回答

2

问题是你正在访问错误的变量。您命名参数ingeter,但您的功能正在访问integer这是一个全局变量。

void DFS(list<short> ingeter, int N) 
//     ^^^^^^^ 
{ 
    if(integer.empty()) 
    // ^^^^^^^ 
    { 
    //... 
    } 
} 
相关问题