vector::shrink_to_fit()
如圖示(圖片來自侯捷的<<STL原始碼剖析>>),vector::capacity()代表了vector從begin()開始的連續空間的記憶體量。
vector::erase(begin() + n)函式可以刪去vector中第n位置的元素。但是刪除之後,vector佔用的capacity不一定減少。C++11增加了shrink_to_fit函式,可以在執行erase之後讓vector的size()等於capacity()。
看下面例子。構造一個vector {1,2,3,4},然後點選deduct按鈕,刪去第3個元素。
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vector>
using namespace std;
vector<int> vec;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString qstrInput = ui->lineEdit->text();
QStringList qstrlst = qstrInput.split(QString(","));
foreach(QString qstrItem, qstrlst)
{
int iVal = qstrItem.toInt();
vec.push_back(iVal);
}
vec.shrink_to_fit();
ui->lineEdit_3->setText(QString().setNum(vec.size()));
ui->lineEdit_4->setText(QString().setNum(vec.capacity()));
ui->label->setText(QString().setNum(*prev(vec.end())));
}
void MainWindow::on_pushButton_2_clicked()
{
int iOldSize = vec.size();
int iIndex = ui->lineEdit_2->text().toInt();
vec.erase(vec.begin() + iIndex);
//vec.shrink_to_fit();
int iTail = *prev(vec.end());
ui->label->setText(QString().setNum(iTail));
ui->lineEdit_3->setText(QString().setNum(vec.size()));
ui->lineEdit_4->setText(QString().setNum(vec.capacity()));
}
效果:capacity 仍然等於4
加入shrink_to_fit函式,capacity 變為3
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <vector>
using namespace std;
vector<int> vec;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
QString qstrInput = ui->lineEdit->text();
QStringList qstrlst = qstrInput.split(QString(","));
foreach(QString qstrItem, qstrlst)
{
int iVal = qstrItem.toInt();
vec.push_back(iVal);
}
vec.shrink_to_fit();
ui->lineEdit_3->setText(QString().setNum(vec.size()));
ui->lineEdit_4->setText(QString().setNum(vec.capacity()));
ui->label->setText(QString().setNum(*prev(vec.end())));
}
void MainWindow::on_pushButton_2_clicked()
{
int iOldSize = vec.size();
int iIndex = ui->lineEdit_2->text().toInt();
vec.erase(vec.begin() + iIndex);
vec.shrink_to_fit();
int iTail = *prev(vec.end());
ui->label->setText(QString().setNum(iTail));
ui->lineEdit_3->setText(QString().setNum(vec.size()));
ui->lineEdit_4->setText(QString().setNum(vec.capacity()));
}
相關文章
- vector
- Vector和Stack
- vector——C++C++
- Paimon Deletion VectorAI
- c++ vectorC++
- Vector擴容
- STL容器---Vector
- vector 使用 上
- Support Vector MachinesMac
- row_vector and col_vector的建立 (Leetcode 807, Leetcode 531)LeetCode
- STL使用篇__vector
- 簡化版vector
- Android中的VectorAndroid
- Vector 原始碼分析原始碼
- C++ STL -- vectorC++
- C++ Vector fundamentalC++
- C++:vector assignC++
- STL---vector(向量)
- python-Vector向量Python
- C++(std::vector)C++
- vector的基本用法
- STL:vector用法總結
- java arrayList vector 區別Java
- C++的vector容器C++
- vector的使用注意點
- Vector + ClickHouse 收集日誌
- vector 二維陣列陣列
- 初探STL容器之Vector
- C++簡單vectorC++
- c++ vector容器、字串C++字串
- 【java】【集合】List、ListIterator、VectorJava
- C++之vector容器C++
- Rust中如何排序Vector?Rust排序
- Vector容器鑲套容器
- Vector3 類簡介
- C++ 容器vector的使用C++
- c++ std::vector 切記C++
- vector訪問與賦值賦值