2

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 ."

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

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

Ayush Chaurasia
  • 165
  • 1
  • 8

2 Answers2

3

To expand upon @Chernobyl's answer: The C++ library puts all of the standard containers, algorithms, etc into a namespace named 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());

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. Then sort(..) would invoke your routine, while std::sort(..) would invoke the one in the standard library. Ditto for find, erase, remove, string, list, and so on.

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
2

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

Or write std::sort every time:

#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();
}
Jablonski
  • 18,083
  • 2
  • 46
  • 47
  • Never advise anyone to use ' using namespace '. – Diego Sánchez Feb 08 '15 at 08:31
  • @DiegoSánchez Maybe you can tell something more? Why? Just because maybe in future OP will write another sort function in another namespace, will write `using namespace` for each one and will get compiler error? – Jablonski Feb 08 '15 at 08:43
  • The above warning applies mainly in header files. You are generally safe to use `using namespace` in source files. – RobbieE Feb 08 '15 at 08:43
  • @Chernobyl: 'using namespace' is considered bad practice, and in many companies it's even forbidden. It floods the global namespace with all sorts of extraneous names. – Diego Sánchez Feb 08 '15 at 08:58
  • @RobbieE, I would never advice that: That wold mean that you will use std::sort when writing a header but sort when writing a module. It introduces naming inconsistencies that make code even harder to maintain. – Diego Sánchez Feb 08 '15 at 09:01
  • That's why I qualified my statement with the word "generally" – RobbieE Feb 08 '15 at 09:13
  • @DiegoSánchez From the book **C++ Coding Standards 101 Rules Guidelines and Best Practices**: _In header files, don't write namespace-level using directives or using declarations; instead, explicitly namespace-qualify all names. [...] In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable._ – user1387866 May 09 '19 at 15:40