#include#include int main() { QMap map; map.insert(1, "one"); map.insert(2, "two"); map.insert(3, "three"); QMap ::const_iterator it = map.constBegin(); while (it != map.constEnd()) { std::cout << it.key() << ": " << it.value().toStdString() << std::endl; ++it; } return 0; }
#includeThis example shows how QMap constBegin can be used with custom objects. A QMap object is created with QString keys and Person values. The Person object has two properties: name and age. In the while loop, the key, full name, and age of each Person object are printed using the QMap constBegin iterator. The QMap constBegin function is part of the Qt library.#include class Person { public: Person(QString name, int age) : name(name), age(age) {} QString getName() const { return name; } int getAge() const { return age; } private: QString name; int age; }; int main() { QMap people; people.insert("Alice", Person("Alice Smith", 35)); people.insert("Bob", Person("Bob Jones", 45)); people.insert("Charlie", Person("Charlie Brown", 25)); QMap ::const_iterator it = people.constBegin(); while (it != people.constEnd()) { const QString name = it.key(); const QString fullName = it.value().getName(); const int age = it.value().getAge(); std::cout << "Name: " << name.toStdString() << std::endl; std::cout << "Full name: " << fullName.toStdString() << std::endl; std::cout << "Age: " << age << std::endl; ++it; } return 0; }