Home > Software engineering >  This print function cannot output the value in the deque
This print function cannot output the value in the deque

Time:07-27

The result of the current execution of this program does not display the result. I want to display the values in the even deque and odd deque through the print function. During the debugging process, I found that the value in the deque already exists, but the print function will end in the middle.

#include <list>
#include <deque>
void print(std::deque<int>::iterator beg, std::deque<int>::iterator end);
int main()
{
    std::list<int> lint{1, 2, 3, 4, 5, 6, 7, 8, 9};
    std::list<int>::iterator itor;
    std::deque<int> dint1, dint2;
    itor = lint.begin();
    for (; itor != lint.end(); itor  )
    {
        if (*itor % 2 == 0)
        {
            dint2.push_back(*itor);
        }
        else
            dint1.push_back(*itor);
    }
    std::deque<int>::iterator itorde1;
    std::deque<int>::iterator itorde2;
    itorde1 = dint1.begin();
    itorde2 = dint1.end();
    std::deque<int>::iterator itorde3;
    std::deque<int>::iterator itorde4;
    itorde3 = dint2.begin();
    itorde4 = dint2.end();
    print(itorde1, itorde2);
    print(itorde3, itorde4);
   
}
void print(std::deque<int>::iterator beg, std::deque<int>::iterator end){
    while (beg == end)
    {
        std::cout<< *beg;
        beg  ;
    }
    
} ```

CodePudding user response:

You have an error in the print function, the condition should be beg != end. Here is the function:

void print(std::deque<int>::iterator beg, std::deque<int>::iterator end){
    while (beg != end)
    {
        std::cout << *beg;
        beg  ;
    }
    std::cout << "\n";
}
  •  Tags:  
  • c
  • Related