QVector学习实践
本文主要包含关于QVector 的以下基础用法。可运行代码查看运行结果,对照结果进行学习。
程序实践
#include <QVector>
#include <iostream>
#include <QString>
void showElement(QVector<int> a)
{
std::cout << "---------------------------------------"<<std::endl;
std::cout<<"vector element:"<<std::endl;
for(int i=0;i<a.size();i++){
std::cout << a[i] << std::endl;
}
std::cout << "---------------------------------------"<<std::endl;
}
void showOnlyRead(QVector<int> a ){
std::cout << "---------------------------------------"<<std::endl;
for(int i = 0;i<a.size();i++){
std::cout<< a.at(i)<<std::endl;
}
std::cout << "---------------------------------------"<<std::endl;
}
void showElement_string(QVector<QString> a)
{
QString s;
std::cout << "---------------------------------------"<<std::endl;
std::cout<<"vector element:"<<std::endl;
for(int i=0;i<a.size();i++){
printf("%s\n",a[i].toStdString().data());
}
std::cout << "---------------------------------------"<<std::endl;
}
int main(int argc, char *argv[])
{
QVector<int> vector0;
QVector<int> vector1(100);
std::cout<<"vector0 size:"<<vector0.size()<<std::endl;
std::cout<<"vector1 size:"<<vector1.size()<<std::endl;
std::cout << "---------------------------------------"<<std::endl;
vector1.resize(10);
std::cout<<"vector1 size:"<<vector1.size()<<std::endl;
std::cout << "---------------------------------------"<<std::endl;
vector1.fill(10);
std::cout << "Before clear"<<std::endl;
showElement(vector1);
if(vector1.empty()){
std::cout << "vector1 is empty"<<std::endl;
}
vector1.clear();
std::cout << "After clear"<<std::endl;
std::cout<<"vector1 size:"<<vector1.size()<<std::endl;
if(vector1.empty()){
std::cout << "vector1 is empty"<<std::endl;
}
QVector<int> vector3(3,2);
QVector<int> vector2(3,1);
QVector<int> vector4;
vector4 << 1<<2<<3<<4;
std::cout << "vector4";
showOnlyRead(vector4);
QVector<int> vectorf(3);
vectorf.fill(4);
std::cout << "vectorf";
showOnlyRead(vectorf);
std::cout << "vector3";
showOnlyRead(vector3);
std::cout << "vector2";
showElement(vector2);
QVector<QString> vector8;
vector8<<"John"<<"Marry"<<"Tom"<<"Bill"<<"Jessy"<<"Michel"<<"Tom"<<"Bill";
showElement_string(vector8);
int Tom_index = vector8.indexOf("Tom",0);
int Tom_index_2 = vector8.indexOf("Tom",4);
std::cout << "Tom index is :"<<Tom_index<<std::endl;
std::cout << "Tom index_2 is :"<<Tom_index_2<<std::endl;
std::cout << "---------------------------------------"<<std::endl;
QVector<int> vector9;
vector9 << 10 << 20 << 30;
std::cout << "vector9"<<std::endl;
std::cout << "first element :"<<vector9.first()<<std::endl;
std::cout << "end element :"<<vector9.end()<<std::endl;
std::cout << "---------------------------------------"<<std::endl;
QVector<int> vector5;
vector5 << 1<<2<<3<<4;
vector5.insert(2,8);
std::cout << "vector5";
showOnlyRead(vector5);
QVector<int> vector6;
vector6 << 1<<2<<3<<4;
vector6.prepend(10);
vector6.prepend(11);
vector6.prepend(12);
std::cout << "vector6";
showOnlyRead(vector6);
QVector<int> vector7;
vector7 << 1<<2<<3<<4;
vector7.append(7);
vector7.append(8);
vector7.append(9);
std::cout << "vector7";
showOnlyRead(vector7);
QVector<int> vector10;
vector10 << 10 << 20 << 30;
std::cout << "vector10"<<std::endl;
vector10.push_back(40);
vector10.push_front(50);
showElement(vector10);
QVector<int> vector11;
vector11 << 11 << 22 << 33;
std::cout << "vector11"<<std::endl;
vector11.pop_back();
vector11.pop_front();
showElement(vector11);
QVector<int> vector12;
vector12 << 1 << 2 << 3 << 4;
std::cout << "vector12"<<std::endl;
vector12.remove(1);
showElement(vector12);
return 0;
}
运行结果:
vector0 size:0
vector1 size:100
---------------------------------------
vector1 size:10
---------------------------------------
Before clear
---------------------------------------
vector element:
10
10
10
10
10
10
10
10
10
10
---------------------------------------
After clear
vector1 size:0
vector1 is empty
vector4---------------------------------------
1
2
3
4
---------------------------------------
vectorf---------------------------------------
4
4
4
---------------------------------------
vector3---------------------------------------
2
2
2
---------------------------------------
vector2---------------------------------------
vector element:
1
1
1
---------------------------------------
---------------------------------------
vector element:
John
Marry
Tom
Bill
Jessy
Michel
Tom
Bill
---------------------------------------
Tom index is :2
Tom index_2 is :6
---------------------------------------
vector9
first element :10
end element :0x5596539ded84
---------------------------------------
vector5---------------------------------------
1
2
8
3
4
---------------------------------------
vector6---------------------------------------
12
11
10
1
2
3
4
---------------------------------------
vector7---------------------------------------
1
2
3
4
7
8
9
---------------------------------------
vector10
---------------------------------------
vector element:
50
10
20
30
40
---------------------------------------
vector11
---------------------------------------
vector element:
22
---------------------------------------
vector12
---------------------------------------
vector element:
1
3
4
---------------------------------------
|