vendredi 13 décembre 2019

How we can move current element of deque at the end and pick next element

I am new in c++ and I am using deque for create a queue of files . I read the directory and put all files of directory in deque . Please see below code for create a deque :

struct dequeFiles {
        int Num;
        std::string name;
    };

Above is the struct of deque . name stores the file name and Num stores the iteration of that file .

std::deque<dequeFiles>  mydeque;

Above is the declaration of deque .

path Dir(Path);
for (auto i = directory_iterator(Dir); i != directory_iterator(); i++) {
    if (!is_directory(i->path())) {
        if(i->path().filename_is_dot() == false && i->path().filename_is_dot_dot() == false)
            mydeque.push_front({0, i->path().mydeque().string()});
    } else {
        continue;
    }
}

Above code will create deque of files and zero initialization of Num(numbers) of file iteration.

for (int i = 0 ;  i < 4 ; i++){

    for (deque<dequeFiles>::iterator it = mydeque.begin(); it != mydeque.end(); ) {

         std::cout << it->name << std::endl;

         if(it->Num >= 3){
            std::cout << it->name << "---" << it->Num << "---" << "This node exceeds 3 turns So moved it at the last and picked next node" << std::endl; 
             mydeque.emplace_front(*it);
             it = mydeque.erase(it);

        }else
            it++;
    }

    std::cout << "-------------------------------\n" << std::endl;
}

Issue : Above code works fine but sometimes I am getting segmentation fault and double free or corruption (fasttop) . There have two for loops during 3 turn of first for loop sometimes filename is missing but it holds the Num value after that I am getting these errors .

So, anyone who is familiar with deque , please help me , How I can move current element of deque at the last as per my code properly. Thank you !!

Aucun commentaire:

Enregistrer un commentaire