zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Qt QVector常见使用方法

Qt方法 常见 使用
2023-09-14 09:05:12 时间

  仅在此简单介绍QVector的一些常见函数,有兴趣的可以查下QT,在QT中介绍的很详细

构造函数,QVector的构造函数很多样化,常见的有

1 QVector()  无参的构造函数
2 
3 QVector(int size) 构造一个大小为size个 值为默认值的一个vector
4 
5 QVector(int size,const T &value) 构造一个大小为size个 值为T &value的一个vector
6 
7 QVector(const QVector<T> &other)构造一个值为QVector<T> &other的vector
 1 //  将元素插入到vector的末尾
 2 
 3 void append(const T &value)  
 4 
 5 void append(const QVector<T> &value)
 6 
 7 void push_back(const T &value)
 8 
 9 void push_back(const QVector<T> &value)
10 
11 //  将元素插入到vector的开始
12 
13 void prepend(const T &value)  
14 
15 void prepend(const QVector<T> &value)
16 
17 void push_front(const T &value)
18 
19 void push_front(const QVector<T> &value)
20 
21 等同于vector.insert(0, value);
22 
23 //  将元素插入到vector的任意位置
24 
25 void insert(int i, const T &value)  将元素插入到i位置,i从0开始计算
26 
27 void insert(int i, int count, const T &value) 从i位置开始插入count个T &value类型元素
28 
29  
30 
31 //  删除元素
32 
33 QVector::iterator erase(QVector::iterator pos) 从vector中移除pos对应的元素
34 
35 void remove(int i, int count)  从vector中移除从 i开始的count个元素  
36 
37 void pop_back()  删除vector中最后一个元素
38 
39 void pop_front()  删除vector中第一个元素
40 
41  
42 
43 //  改变i位置元素的值
44 
45 void replace(int i, const T &value)
46 
47  
48 
49 //  使用迭代器进行查找
50 
51 QVector::iterator begin()  返回一个STL类型的迭代器指针指向vector的第一个元素
52 
53 QVector::iterator end()  返回一个STL类型的迭代器指针指向vector的最后一个元素后面的假想元素
54 
55  //  capacity,reserve,count,length,size的比较
56 
57 int capacity() const    返回vector客观上的容量
58 
59 void reserve(int size)  扩展至少size大小的内存
60 
61 int count() const    返回vector中的元素个数
62 
63 int length() const    等同于count()
64 
65 int size() const     等同于count()
66 
67  
68 
69 QVector::reference QVector::back() 返回vector中的最后一个元素的引用 等同于T &QVector::last()
70 
71 T &QVector::front()  返回vector中的第一个元素的引用 等同于T & first()
72 
73  
74 
75 void clear() 移除vector中的所有元素
76 
77 bool empty() const 判断vector是否为空,如果为空返回true,else返回false
78 
79  
80 
81 int count(const T &value) const    返回T &value类型元素在vector中的个数
82 
83 int indexOf(const T &value, int from=...) const  返回 value在vector中T &value类型元素的位置
84 
85  
86 
87 const T &at(int i)const  返回 i位置元素 在vector的index
88 
89 等同于 T QVector::value(int i) const