Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

This print function cannot output the value in the deque

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++;
    }
    
} ```

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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";
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading