Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;为什么;deque迭代器不可解引用“;_C++ - Fatal编程技术网

C++ C++;为什么;deque迭代器不可解引用“;

C++ C++;为什么;deque迭代器不可解引用“;,c++,C++,我正在做一个样本测试 #include <stdexcept> #include <iostream> #include <deque> class TrainComposition { //std::vector<int> wagons; public: std::deque<int> wagons; void attachWagonFromLeft(int wagonId) {

我正在做一个样本测试

    #include <stdexcept>
#include <iostream>
#include <deque>

class TrainComposition
{
    //std::vector<int> wagons;

public:

    std::deque<int> wagons;

    void attachWagonFromLeft(int wagonId)
    {
        wagons.push_front(wagonId);
    }

    void attachWagonFromRight(int wagonId)
    {
        wagons.push_back(wagonId);
    }

    int detachWagonFromLeft()
    {
        std::deque<int>::iterator it = wagons.begin();
        wagons.pop_front();
        return *it;
    }

    int detachWagonFromRight()
    {
        std::deque<int>::iterator it = wagons.end()-1;
        wagons.pop_back();
        return *it;
    }
};

#ifndef RunTests
int main()
{
    TrainComposition tree;
    tree.attachWagonFromLeft(7);
    tree.attachWagonFromLeft(13);
    std::cout << tree.detachWagonFromRight() << "\n"; // 7 
    std::cout << tree.detachWagonFromLeft() << "\n"; // 13
    return 0;
}
#endif
“列车组合是通过从左侧和右侧连接和分离货车来构建的。例如,如果我们先从左侧连接货车7,然后从左侧连接货车13,再从左侧连接货车13,我们得到两辆货车的组合(从左到右连接13和7).现在,第一辆可以从右侧分离的货车是7辆,第一辆可以从左侧分离的货车是13辆。实施一个模拟此问题的列车组合。”

    #include <stdexcept>
#include <iostream>
#include <deque>

class TrainComposition
{
    //std::vector<int> wagons;

public:

    std::deque<int> wagons;

    void attachWagonFromLeft(int wagonId)
    {
        wagons.push_front(wagonId);
    }

    void attachWagonFromRight(int wagonId)
    {
        wagons.push_back(wagonId);
    }

    int detachWagonFromLeft()
    {
        std::deque<int>::iterator it = wagons.begin();
        wagons.pop_front();
        return *it;
    }

    int detachWagonFromRight()
    {
        std::deque<int>::iterator it = wagons.end()-1;
        wagons.pop_back();
        return *it;
    }
};

#ifndef RunTests
int main()
{
    TrainComposition tree;
    tree.attachWagonFromLeft(7);
    tree.attachWagonFromLeft(13);
    std::cout << tree.detachWagonFromRight() << "\n"; // 7 
    std::cout << tree.detachWagonFromLeft() << "\n"; // 13
    return 0;
}
#endif
我使用std::deque修改了代码,但在构建代码时,出现了一个错误:“deque迭代器不可解引用”。为什么会发生此错误以及如何解决它

    #include <stdexcept>
#include <iostream>
#include <deque>

class TrainComposition
{
    //std::vector<int> wagons;

public:

    std::deque<int> wagons;

    void attachWagonFromLeft(int wagonId)
    {
        wagons.push_front(wagonId);
    }

    void attachWagonFromRight(int wagonId)
    {
        wagons.push_back(wagonId);
    }

    int detachWagonFromLeft()
    {
        std::deque<int>::iterator it = wagons.begin();
        wagons.pop_front();
        return *it;
    }

    int detachWagonFromRight()
    {
        std::deque<int>::iterator it = wagons.end()-1;
        wagons.pop_back();
        return *it;
    }
};

#ifndef RunTests
int main()
{
    TrainComposition tree;
    tree.attachWagonFromLeft(7);
    tree.attachWagonFromLeft(13);
    std::cout << tree.detachWagonFromRight() << "\n"; // 7 
    std::cout << tree.detachWagonFromLeft() << "\n"; // 13
    return 0;
}
#endif
#包括
#包括
#包括
班列组成
{
//std::矢量货车;
公众:
标准:德克货车;
void attachWagonFromLeft(int wagonId)
{
货车。向前推(wagonId);
}
void attachWagonFromRight(int-wagonId)
{
四轮马车。向后推(四轮马车);
}
int detachWagonFromLeft()
{
std::deque::iterator it=wagons.begin();
四轮马车;
归还它;
}
int detachWagonFromRight()
{
std::deque::iterator it=wagons.end()-1;
四轮马车;
归还它;
}
};
#ifndef运行测试
int main()
{
列车组合树;
树。从左(7)起连接货车;
树。从左(13)到右(13);

std::cout在
detachWagonFromLeft()
中的这一指令序列是一个问题:

    #include <stdexcept>
#include <iostream>
#include <deque>

class TrainComposition
{
    //std::vector<int> wagons;

public:

    std::deque<int> wagons;

    void attachWagonFromLeft(int wagonId)
    {
        wagons.push_front(wagonId);
    }

    void attachWagonFromRight(int wagonId)
    {
        wagons.push_back(wagonId);
    }

    int detachWagonFromLeft()
    {
        std::deque<int>::iterator it = wagons.begin();
        wagons.pop_front();
        return *it;
    }

    int detachWagonFromRight()
    {
        std::deque<int>::iterator it = wagons.end()-1;
        wagons.pop_back();
        return *it;
    }
};

#ifndef RunTests
int main()
{
    TrainComposition tree;
    tree.attachWagonFromLeft(7);
    tree.attachWagonFromLeft(13);
    std::cout << tree.detachWagonFromRight() << "\n"; // 7 
    std::cout << tree.detachWagonFromLeft() << "\n"; // 13
    return 0;
}
#endif
std::deque<int>::iterator it = wagons.begin();
wagons.pop_front();
return *it;
detachWagonFromRight()
函数中存在类似的问题,可以修复为:

    #include <stdexcept>
#include <iostream>
#include <deque>

class TrainComposition
{
    //std::vector<int> wagons;

public:

    std::deque<int> wagons;

    void attachWagonFromLeft(int wagonId)
    {
        wagons.push_front(wagonId);
    }

    void attachWagonFromRight(int wagonId)
    {
        wagons.push_back(wagonId);
    }

    int detachWagonFromLeft()
    {
        std::deque<int>::iterator it = wagons.begin();
        wagons.pop_front();
        return *it;
    }

    int detachWagonFromRight()
    {
        std::deque<int>::iterator it = wagons.end()-1;
        wagons.pop_back();
        return *it;
    }
};

#ifndef RunTests
int main()
{
    TrainComposition tree;
    tree.attachWagonFromLeft(7);
    tree.attachWagonFromLeft(13);
    std::cout << tree.detachWagonFromRight() << "\n"; // 7 
    std::cout << tree.detachWagonFromLeft() << "\n"; // 13
    return 0;
}
#endif
int temp = wagons.back();
wagons.pop_back();
return temp;
wagons.end()。