QStringList fruits = {"apple", "banana", "orange"}; QString lastFruit = fruits.takeLast(); qDebug() << "The last fruit is: " << lastFruit; // Output: The last fruit is: orange
QStringList fruits = {"apple"}; QString lastFruit = fruits.takeLast(); if (fruits.isEmpty()) { qDebug() << "Empty list!"; }
QStringList words = {"Hello", "world", "from", "OpenAI"}; QString lastTwo = words.takeLast() + " " + words.takeLast(); qDebug() << "The last two elements are: " << lastTwo; // Output: The last two elements are: OpenAI fromThe QStringList class is a part of the QtCore module in Qt library.