Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » QT-C++ » Example of QVector

Example of QVector in QT-C++

Main.cpp


#include<iostream>
#include<QVector>

using namespace std;

int main()
{
QVector<int> vec = {1, 2, 3, 4, 5};

cout<<"The size of the vector is: " << vec.size() << endl;

cout<<"The first item is: " << vec.first() << endl;
cout<<"The last item is: " << vec.last() << endl;

vec.append(6);  //adding new element at the end
vec.prepend(0); //adding new element at the beginning

cout<<"Elements are: ";

for (int val : vec) { //for each loop
   cout << val << " ";
}
return 0;
}

Project in QT Creator

Project file

Output of the program