firemail
标题:
Qt常见容器使用
[打印本页]
作者:
jimu
时间:
2016-12-7 09:44
标题:
Qt常见容器使用
本帖最后由 jimu 于 2016-12-7 10:35 编辑
1.QVector
QVector<QReadMailWindow *> m_pVecReadMailWindows;
//循环删除
for (int i = 0; i < m_pVecReadMailWindows.size(); i++)
{
if (m_pVecReadMailWindows.at(i) != NULL)
{
delete m_pVecReadMailWindows.at(i);
m_pVecReadMailWindows.erase(m_pVecReadMailWindows.begin() + i);
}
}
复制代码
//添加
QReadMailWindow *rWin = new QReadMailWindow(mailId);
m_pVecReadMailWindows.push_back(rWin);
复制代码
//查询
foreach(QReadMailWindow * rWin, m_pVecReadMailWindows)
{
if (rWin->getCloseStatus())
{
return rWin;
}
}
复制代码
qFind 查询 to implement operator==().
struct UserOperateAction
{
UserOperateAction()
: serverId(DEFAULT_VALUE_ZERO)
, operType(UOAT_UNKNOWE)
{
}
uint32_t serverId;
QString listPath;
QString uid;
int operType;
bool operator == (const UserOperateAction& rhs) const
{
return (serverId == rhs.serverId &&
listPath == rhs.listPath &&
uid == rhs.uid &&
operType == rhs.operType);
}
};
bool queryUserOperAct(const UserOperateAction & stUserOperateAction)
{
QVector<UserOperateAction>::const_iterator itFind = qFind(m_vecUserOperActs.begin(), m_vecUserOperActs.end(), stUserOperateAction);
if (itFind != m_vecUserOperActs.end())
{
return true;
}
return false;
}
bool clearUserOperAct()
{
for (int i = 0; i < m_vecUserOperActs.size(); i++)
{
m_vecUserOperActs.erase(m_vecUserOperActs.begin() + i);
}
}
bool removeUserOperAct(const UserOperateAction & stUserOperateAction)
{
QMutexLocker locker(&m_userOperActMutex);
QVector<UserOperateAction>::iterator itFind = qFind(m_vecUserOperActs.begin(), m_vecUserOperActs.end(), stUserOperateAction);
if (itFind != m_vecUserOperActs.end())
{
m_vecUserOperActs.erase(itFind);
}
return true;
}
2.QList
QMutex m_folderMutex;
QMutexLocker locker(&m_folderMutex);
QList<MailFolderInfo> m_listMailFoldersData;
//查询
for (int i = 0; i < m_listMailFoldersData.size(); ++i)
{
if (m_listMailFoldersData.at(i).parentId == parentId)
{
return m_listMailFoldersData.at(i).rowId;
}
}
//排序
qSort(m_listMailFoldersData.begin(), m_listMailFoldersData.end(), folderSortAlgorithm);
bool folderSortAlgorithm(const MailFolderInfo &mfi1, const MailFolderInfo &mfi2)
{
return mfi1.name < mfi2.name
}
//添加
bool addMailFolder(MailFolderInfo& stMailFolderInfo)
{
QMutexLocker locker(&m_folderMutex);
m_listMailFoldersData.push_back(stMailFolderInfo);
}
//删除
bool CMemoryDBManager::deleteMailFolder(uint32_t folderId)
{
QMutexLocker locker(&m_folderMutex);
for (int i = 0; i < m_listMailFoldersData.size(); ++i) {
if (m_listMailFoldersData.at(i).rowId == folderId){
m_listMailFoldersData.removeAt(i);
break;
}
}
}
//修改
bool updateMailFolderLegalMailCount(uint32_t folderId, uint32_t count)
{
QMutexLocker locker(&m_folderMutex);
for (int i = 0; i < m_listMailFoldersData.size(); ++i) {
if (m_listMailFoldersData.at(i).rowId == folderId){
m_listMailFoldersData
.legalMailCountOnServer = count;
break;
}
}
return true;
}
3.QMap
//查询 不能用 [] 防止引入 新元素
MailHeaderInfo getMailHeader(uint64_t mailId)
{
QMapIterator<uint32_t, MemoryMailData> iter(m_mapMailMemoryData);
while (iter.hasNext()) {
iter.next();
for (int i = 0; i < iter.value().vecMailIndexDatas.size(); ++i) {
if (iter.value().vecMailIndexDatas.at(i).mailId == mailId){
return iter.value().vecMailIndexDatas.at(i);
}
}
}
}
作者:
firemail
时间:
2016-12-30 18:20
QMap 删除元素
for (auto it = map.begin(); it != map.end(); /* don't increment here */) {
if (it.value() % 2 == 1) {
it = map.erase(it);
} else {
++it;
}
}
复制代码
作者:
firemail
时间:
2017-1-3 11:14
QMap添加
bool CMemoryDBManager::addMailRecord(MailHeaderInfo& stMailInfo)
{
QMutexLocker locker(&m_mailMutex);
m_mapMailMemoryData[stMailInfo.folderId].vecMailIndexDatas.push_back(stMailInfo);
regenerateMailListModelData();
return true;
}
复制代码
作者:
firemail
时间:
2017-1-5 17:13
bool CMemoryDBManager::delMailIndex(uint32_t folderId, const QString & uid)
{
QMutexLocker locker(&m_mailIndexMutex);
for (int i = 0; i < m_mapMailIndexData[folderId].vecSyncMailIndexs.size(); ++i) {
if (m_mapMailIndexData[folderId].vecSyncMailIndexs.at(i).uid == uid)
{
m_mapMailIndexData[folderId].vecSyncMailIndexs.removeAt(i);
return true;
}
}
return false;
}
复制代码
作者:
firemail
时间:
2017-1-10 17:11
QList查询 find
QList<SyncMailIndexInfo>::const_iterator itFind = qFind(m_mapMailIndexData[folderId].vecSyncMailIndexs.begin(), m_mapMailIndexData[folderId].vecSyncMailIndexs.end(), stSyncMailIndexInfo);
if (itFind == m_mapMailIndexData[folderId].vecSyncMailIndexs.end())
{
return false;
}
return true;
struct SyncMailIndexInfo
{
SyncMailIndexInfo()
: indexPop(DEFAULT_VALUE_ZERO)
, flags(DEFAULT_VALUE_ZERO)
, date(NOT_SET_VALUE)
, operType(IOT_ADD)
{
}
QString uid; //exchange id
uint32_t indexPop; //兼容 pop
uint32_t flags;
uint64_t date;
IndexOperType operType;
bool operator == (const SyncMailIndexInfo& rhs) const {
return (uid == rhs.uid && operType == rhs.operType);
}
};
复制代码
作者:
Qter
时间:
2017-1-22 10:55
循环排序时注意:如果vec中不含有一条记录,要防止i=0时这种情况还进行排序,就会出异常
//对分组内邮件默认按时间排序
if (m_vecMailGroupModelData.size() > 0) //去掉对i=0时还进行排序
{
for (int i = 0; i < m_vecMailGroupModelData.size(); i++)
{
qSort(m_vecMailGroupModelData[i].vec_mailIds.begin(), m_vecMailGroupModelData[i].vec_mailIds.end(), CMemoryDBManager::compMailTimeDescEx);
}
}
复制代码
作者:
Qter
时间:
2017-1-23 11:53
QSet
QSet<QuerySummaryInfo>::const_iterator iter = setQuerySummarys.constBegin();
while (iter != setQuerySummarys.constEnd()) {
qDebug() << (*iter).mailId << " " << (*iter).data;
++iter;
}
复制代码
作者:
Qter
时间:
2017-1-24 12:23
QMap引入 新元素的问题 由于外部还有一个for循环造成 且索引在一直增大,这样会造成内部数据一直增大,如果没有外部循环 即j是定死的,则不会出现内存暴涨
暴涨版:
for (int j = 0; j < m_mapMailMemoryData.size(); ++j) {
for (int i = 0; i < m_mapMailMemoryData[j].vecMailIndexDatas.size(); ++i) {
if (m_mapMailMemoryData[j].vecMailIndexDatas.at(i).mailId == mailId){
return m_mapMailMemoryData[j].vecMailIndexDatas.at(i);
}
}
}
修改版:
QMapIterator<uint32_t, MemoryMailData> iter(m_mapMailMemoryData);
while (iter.hasNext()) {
iter.next();
for (int i = 0; i < iter.value().vecMailIndexDatas.size(); ++i) {
if (iter.value().vecMailIndexDatas.at(i).mailId == mailId){
return iter.value().vecMailIndexDatas.at(i);
}
}
}
复制代码
作者:
Qter
时间:
2017-2-15 11:53
QList删除
for (auto it = childItems.begin(); it != childItems.end(); /* don't increment here */) {
if ((*it)->stItemData.id == itemData.id)
{
delete (*it);
it = childItems.erase(it);
break;
}
else
{
++it;
}
}
复制代码
欢迎光临 firemail (http://firemail.wang:8088/)
Powered by Discuz! X3