简体   繁体   English

如何在Qt中使用STL算法?

[英]How to use STL algorithms in Qt?

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 ." 在阅读“ Qt 4的c ++ gui编程第二版”时,我遇到了这个话题:“ STL标头提供了更完整的通用算法集。这些算法可用于Qt容器和STL容器。如果STL实现是在所有平台上都可用,当Qt缺乏等效算法时,可能没有理由避免使用STL算法。”

It states that the generic algorithms of STL(which is defined in "algorithm" header) can be used with Qt containers as well . 它指出STL的通用算法(在“ algorithm”标头中定义)也可以与Qt容器一起使用。 But when I run the following code it shows an error that "sort: identifier not found" : 但是,当我运行以下代码时,它显示了一个错误“ sort:not found identifier”:

#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();
}

Is there any way to fix it without using Qt Algorithms? 没有使用Qt算法就可以解决它吗?

To expand upon @Chernobyl's answer: The C++ library puts all of the standard containers, algorithms, etc into a namespace named std . 为了扩展@Chernobyl的答案:C ++库将所有标准容器,算法等放入名为std的命名空间中。 This means that to use them you have to either bring them into the global namespace ( using namespace std; or using std::sort ) or just qualify the name yourself std::sort(vec.begin(), vec.end()); 这意味着要使用它们,您必须将它们带入全局名称空间( using namespace std;using std::sort ),或者只是自己限定名称std::sort(vec.begin(), vec.end());

Either one works fine. 两种都可以。 The reason for this is otherwise all of the identifiers in the standard library would effectively become "reserved words", and you would be unable to (easily) use them in your programs for your own use. 这样做的原因是,否则标准库中的所有标识符都将有效地成为“保留字”,并且您将无法(轻松地)在程序中使用它们以供自己使用。 For example, there's no reason you can't write a function called sort yourself, which sorts a particular data structure. 例如,没有理由您不能自己编写一个名为sort的函数,该函数sort特定的数据结构进行排序。 Then sort(..) would invoke your routine, while std::sort(..) would invoke the one in the standard library. 然后sort(..)将调用您的例程,而std::sort(..)将调用标准库中的例程。 Ditto for find , erase , remove , string , list , and so on. 同上finderaseremovestringlist等。

This function locates in std namespace, so just write: 此函数位于std名称空间中,因此只需编写:

#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();
}

Or write std::sort every time: 或每次写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();
}

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

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