繁体   English   中英

将 std::vector 复制到 qvector

[英]copying a std::vector to a qvector

我尝试使用以下方法将一个向量的内容复制到QVector

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

但是目标QVector仍然是空的。

有什么建议么 ?

看看:

std::vector<T> QVector::toStdVector () const

QVector<T> QVector::fromStdVector ( const std::vector<T> & vector ) [static]

来自文档

如果要使用std::vector的内容创建新的QVector ,则可以使用以下代码作为示例:

   std::vector<T> stdVec;
   QVector<T> qVec = QVector<T>::fromStdVector(stdVec);

'fromStdVector' 最近已被明确标记为已弃用。 使用以下代码:

std::vector<...> stdVec;

// ...       

QVector<...> qVec = QVector<...>(stdVec.begin(), stdVec.end());

正如其他答案所提到的,您应该使用以下方法将QVector转换为std::vector

std::vector<T> QVector::toStdVector() const

以及将std::vector转换为QVector的以下静态方法:

QVector<T> QVector::fromStdVector(const std::vector<T> & vector)

以下是如何使用QVector::fromStdVector (取自此处):

std::vector<double> stdvector;
stdvector.push_back(1.2);
stdvector.push_back(0.5);
stdvector.push_back(3.14);

QVector<double> vector = QVector<double>::fromStdVector(stdvector);

不要忘记在第二个QVector之后指定类型(它应该是QVector<double>::fromStdVector(stdvector) ,而不是QVector::fromStdVector(stdvector) )。 不这样做会给你一个恼人的编译器错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM