copying a std::vector to a qvector

后端 未结 4 1211
囚心锁ツ
囚心锁ツ 2021-02-20 04:40

I tried copy content of one vector to a QVector using the following

std::copy(source.begin(), source.end(), dest.begin());

However

4条回答
  •  广开言路
    2021-02-20 05:18

    As the other answers mentioned, you should use the following method to convert a QVector to a std::vector:

    std::vector QVector::toStdVector() const
    

    And the following static method to convert a std::vector to a QVector:

    QVector QVector::fromStdVector(const std::vector & vector)
    

    Here is an example of how to use QVector::fromStdVector (taken from here):

    std::vector stdvector;
    stdvector.push_back(1.2);
    stdvector.push_back(0.5);
    stdvector.push_back(3.14);
    
    QVector vector = QVector::fromStdVector(stdvector);
    

    Don't forget to specify the type after the second QVector (it should be QVector::fromStdVector(stdvector), not QVector::fromStdVector(stdvector)). Not doing so will give you an annoying compiler error.

提交回复
热议问题