2015-01-15 108 views
1
public:vector<vector<string>> cont; 
void memContent(string path, int f){ 
    DIR *dir; 
    struct dirent *ent; int j = 0; 
    if ((dir = opendir(path.c_str())) != NULL) { 
     while ((ent = readdir(dir)) != NULL) { 
      for (int i = 0; i < ext_no; i++) 
      if (strstr(ent->d_name, ext[i].c_str())) 
      { 

       cont[f].push_back(ent->d_name); 
      } 
     } 
     closedir(dir); 
    } 
} 
main(){ 

for (int i = 0; i < f.dir_no; i++) 
    f.memContent(f.dir[i], i); 
} 

我有一个向量应该从一些文件夹中获取所有文件,但我不断收到有关向量大小的错误; 顺便说一句:我用“F”要知道当前文件夹和我在其他载体C++向量下标超出范围dirent

+0

别当您push_back时添加索引。只要做cont.push_back – Chiel

+0

你确定吗?我的意思是矢量的结构是cont [文件夹] [文件] ..而我只是试图push_back文件的名称为每个文件夹 – hallelujah

+0

@hallelujah你确保'矢量'至少是'调整大小)'适当地存储在索引'f'的东西? –

回答

0

的路径,你需要确保你的cont载体包含每个目录中的一个矢量循环之前:

cont.resize(dir_no) 
+0

工作!谢谢 ! – hallelujah