Qt模块化笔记之core——容器类归纳

容器类用于容纳其它类型的数据或者对象,如一个列表中有多个字符串。

C++中的容器类包括“顺序存储结构”和“关联存储结构”,前者包括vector,list,deque等;后者包括set,map,multiset,multimap等。

与标准C++一样,Qt提供了类似的类,列表如下:


Class Summary
QList<T> 最常用的窗口类,能应付大多数情况。<T>指它容纳的数据类型, 如QString、int等,通过整数索引能访问它的元素。

 QStringList 继承自:QList<QString>,专门用于容纳字符串。

QLinkedList<T> 链表,通迭代器访问元素。当将元素插入大量数据中间中某个位置时,效率更高。
QVector<T> c++中vector类的重新实现。数据连续地存储在内存中。在头部或中间插入数据时,效率低下,因要移动大量数据。
QStack<T> QVector的子类。 操作数据时,后进先出,似乎与C++中的队列相同。 "last in, first out" (LIFO)
QQueue<T> QList的子类。先进先出。"first in, first out" (FIFO). 
QSet<T> 能快速查找,查看它和c++的set的区别:http://blog.csdn.net/small_qch/article/details/7384966
QMap<Key, T> 关联式容器映射,一个key对应着一个T值 QMap按Key的顺序存储数据; 如果顺序不重要,QHash更快更适合。
QMultiMap<Key, T> QMap的子类。一个key可对应着多个T值
QHash<Key, T>QMap几乎一致, 但大大提升查找速度. QHash以随意顺序存储数据
QMultiHash<Key, T> QHash子类,一个key可对应着多个T值

列表中, QList<T>中的T表示所要存储的数据类型,如字符串,可用 QList<QString>,整数的话,可以 QList<int>,其它依此类推.另一类 QMap<Key, T>中的Key也是可以多种类型的

至于在实际应用中,选用哪个:一般速度与存储空间是不能兼得的。

下面看典型的两个:QList<T>与QMap<Key, T>的使用:

QList的模式:

QList<QString> list;
list << "winxp" << "win7" << "mac"<<"kubuntu";

可看成存储后的效果如下,左边数字一栏是索引(整数,自动添加),右边是所对应的值:

0winxp
1win7
2mac
3kubuntu
取值时可用:

QString a = list.at(0);//返回winxp
QString b = list[2];//返回mac
其它如first()、last()函数都可以便捷地取到相应的第一位和最后一位的值

如果删除或插入一行,索引所对应的值都将改变。

如删除"win7",则:

0winxp
1mac
2kubuntu

QMap<Key, T>也可以实现上面 QList<T>的功能:

QMap<int, QString> map;
map.insert(0,"winxp");
map.insert(1,"win7" );
map.insert(2,"mac");
map.insert(3,"kubuntu");
取值时,可用:

QString a = map["2"];//返回"mac"




查看 QList<T>的公有函数,它提供了大量的方法来操作它所容纳的元素

	QList()
	QList(const QList<T> & other)
	QList(QList<T> && other)
	QList(std::initializer_list<T> args)
	~QList()
void	       append(const T & value)//往容器最后添加一个数据或对象
void        append(const QList<T> & value)
void 	prepend(const T & value)//在第一个前添加
void 	push_back(const T & value)//功能与append相同
void        push_front(const T & value)//与prepend相同
void 	insert(int i, const T & value)//在第i位置插入

void 	replace(int i, const T & value)//将第i位的数据替换成value
void 	pop_back()//pop意为拿出,即删除尾部一个数据,以下几个删除功能将对应位置元素删除后,元素都重新索引
void 	pop_front()//删除头部一个
int    	removeAll(const T & value)//清空与value值相同的元素,返回删除个数
void 	removeAt(int i)//删除某个i位置的元素
bool 	removeOne(const T & value)//删除第一个匹配到的值
void 	 removeFirst()//删除第一个元素
void 	removeLast()//删除最后一个元素
void 	reserve(int alloc)//未知,求教

void 	move(int from, int to)//移动元素,效果如:list << "A" << "B" << "C" << "D" << "E" << "F";list.move(1, 4);最终list: ["A", "C", "D", "E", "B", "F"]
void 	clear()

bool 	contains(const T & value) const//判断是否包含某值
int	count(const T & value) const
int	count() const//返回list中元素个数
int	length() const//与count相同
bool	empty() const
bool	endsWith(const T & value) const//是否以某值结尾
int		indexOf(const T & value, int from = 0) const//某值的索引
bool		isEmpty() const
int		lastIndexOf(const T & value, int from = -1) const
QList<T>	mid(int pos, int length = -1) const
int		size() const//与count相同
bool		startsWith(const T & value) const
void		swap(QList<T> & other)
void		swap(int i, int j)

T &	operator[](int i)//以下标方式得到某索引所对应的值,如list[0]表示第一个位置的值
T	takeAt(int i)//删除i索引对应的值,并返回该值,以下两个类似
T	takeFirst()
T	takeLast()
T &	back()//得到列表末尾的值
T &	first()
T &	front()
T &	last()
T	value(int i) const
T	value(int i, const T & defaultValue) const
const T &	operator[](int i) const
const T &	at(int i) const
const T &	back() const
const T &	first() const
const T &	front() const
const T &	last() const
QSet<T>		toSet() const
std::list<T>	toStdList() const
QVector<T>	toVector() const
QList<T>	operator+(const QList<T> & other) const
QList<T> &	operator+=(const QList<T> & other)
QList<T> &	operator+=(const T & value)
QList<T> &	operator<<(const QList<T> & other)
QList<T> &	operator<<(const T & value)
QList<T> &	operator=(const QList<T> & other)
QList &		operator=(QList<T> && other)
bool		operator==(const QList<T> & other) const
bool		operator!=(const QList<T> & other) const

const_iterator	begin() const
const_iterator	cbegin() const
const_iterator	cend() const
const_iterator	constBegin() const
const_iterator	constEnd() const
const_iterator	end() const
iterator	end()
iterator	erase(iterator pos)
iterator	erase(iterator begin, iterator end)
iterator	begin()
iterator	insert(iterator before, const T & value)



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值