如何在 Qt 中使用 STL 算法? [英] How to use STL algorithms in Qt?

查看:43
本文介绍了如何在 Qt 中使用 STL 算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读c++ gui 编程 eith Qt 4,第二版"时,我遇到了这个话题:STL 头文件提供了一套更完整的通用算法.这些算法可以在 Qt 容器和 STL 容器上使用.如果 STL 实现在您的所有平台上都可用,那么在 Qt 时可能没有理由避免使用 STL 算法缺乏等效的算法."

While reading "c++ gui programming eith Qt 4 , second edition " I came across this topic : "The STL header provides a more complete set of generic algorithms. these algorithms can be used on Qt containers as well as STL containers. If STL implementations are available on all your platforms, there is probably no reason to avoid using the STL algorithms when Qt lacks an equivalent algorithm ."

它指出 STL 的通用算法(在算法"标题中定义)也可以与 Qt 容器一起使用.但是当我运行以下代码时,它显示一个错误排序:未找到标识符":

It states that the generic algorithms of STL(which is defined in "algorithm" header) can be used with Qt containers as well . But when I run the following code it shows an error that "sort: identifier not found" :

#include <QApplication>
#include <algorithm>
#include <QVector>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 QVector<int>vec{9,6,10,5,7};
 sort(vec.begin(),vec.end());
    return a.exec();
}

有没有不使用 Qt 算法的方法来修复它?

Is there any way to fix it without using Qt Algorithms?

推荐答案

这个函数位于 std 命名空间中,所以只写:

This function locates in std namespace, so just write:

#include <QApplication>
#include <algorithm>
#include <QVector>
using namespace std;//new line!

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 QVector<int>vec{9,6,10,5,7};
 sort(vec.begin(),vec.end());
    return a.exec();
}

或者每次都写std::sort:

#include <QApplication>
#include <algorithm>
#include <QVector>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
 QVector<int>vec{9,6,10,5,7};
 std::sort(vec.begin(),vec.end());
    return a.exec();
}

这篇关于如何在 Qt 中使用 STL 算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆