QT之QVector的使用


1、頭文件

#include <QVector>

2、使用格式

QVector<類型> 對象
例如:
QVector<int> a;
QVector<QString> b;

3、向容器中添加內容

a.append(1)
a.append(2)

a.insert(0, 3)    //第一個參數代表的是插入數據的位置,第二個代表插入數據

4、循環打印容器內容

// 方式一
for(int i = 0; i < a.size(); i++){
    qDebug() << a.at(i);  
}

// 方式二
for(auto first = b.begin(); first != b.end(); first++){
    qDebug() << *first;
}

// 方式三
QVector<int>::iterator iter;
for (iter=b.begin();iter!=b.end();iter++){
    qDebug() <<  *iter << "\0";
}

5、刪除元素

b.remove(1);    // 參數代表的是位置
b.pop_back();   // 刪除末尾元素    
b.pop_front();   // 刪除開始位置元素

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM