vector容器用swap()与pop_back实现删除操作
时间: 2024-09-27 12:08:44 浏览: 96
在C++中,`vector`容器是一个动态数组,它可以自动调整大小以适应元素的数量。如果你想从`vector`中删除最后一个元素,通常有几种方法可以实现:
1. 使用`pop_back()`函数:这个内置函数会直接移除并返回`vector`的最后一个元素,同时将剩余元素向左移动一位,使得`vector`的长度减一。例如:
```cpp
std::vector<int> vec;
// ...添加元素
vec.pop_back(); // 删除最后一个元素
```
2. 使用`swap()`函数结合`erase()`:虽然不是直接删除,但可以先将最后一个元素与第一个元素交换位置,然后使用`erase()`函数移除最后一个元素。这一步操作类似于手动的栈操作。示例:
```cpp
std::vector<int> vec;
// ...添加元素
int last = vec.back(); // 获取最后一个元素
vec.swap(vec[0]); // 将最后一个元素交换到第一位
vec.erase(vec.end() - 1); // 删除原最后一位(现在是第一个)
```
这两种方法都可以达到删除`vector`末尾元素的目的,但`pop_back()`更为简洁直观。
相关问题
#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; struct Node { Node(double d, Node* l = NULL, Node* r = NULL, Node* f = NULL) :data(d), left(l), right(r), father(f) {} double data; Node* father, * left, * right; //父,左右孩子 string code; //存储编码 }; typedef Node* Tree; //通过中序,构建编码 void creatCode(Node* node, string s) { if (node != NULL) { creatCode(node->left, s + '0'); if (node->left == NULL && node->right == NULL) //是叶子节点就更新编码 node->code = s; creatCode(node->right, s + '1'); } } int main() { vector<double> w; vector<Node*> node; double tmp; Tree tree; cout << "输入权值,数字后紧跟回车结束:"; do { cin >> tmp; w.push_back(tmp); } while (getchar() != '\n'); sort(w.begin(), w.end(), greater<double>()); //降序排序 for (int i = 0; i < w.size(); i++) node.push_back(new Node(w[i])); vector<Node*> out = node; Node* left, * right; do { right = node.back(); node.pop_back(); //取出最小的两个 left = node.back(); node.pop_back(); node.push_back(new Node(left->data + right->data, left, right)); //将新结点(求和)推进数组中 left->father = node.back(); //更新父结点 right->father = node.back(); out.push_back(node.back()); //存储此结点 for (int i = node.size() - 1; i > 0 && node[i]->data > node[i - 1]->data; i--) //从末尾冒泡,排序 swap(node[i], node[i - 1]); } while (node.size() != 1); //构建树结构 tree = node.front(); //剩余的一个结点即根结点 creatCode(tree, ""); printf("结点\t父结点\t左孩子\t右孩子\t编码\n"); for (int i = 0; i < out.size(); i++) printf("%.2lf\t%.2lf\t%.2lf\t%.2lf\t%s\n", out[i]->data, out[i]->father == NULL ? 0 : out[i]->father->data, out[i]->left == NULL ? 0 : out[i]->left->data, out[i]->right == NULL ? 0 : out[i]->right->data, out[i]->code.c_str()); return 0; }根据代码写流程图
```mermaid
graph TD;
A(开始) --> B(定义结构体Node和树Tree);
B --> C(定义函数creatCode);
B --> D(定义主函数main);
D --> E(定义变量w,node,tmp,tree);
E --> F(输入权值,数字后紧跟回车结束);
F --> G(获取输入的权值并降序排序);
G --> H(依次创建Node结构体并存储到node数组中);
H --> I(将node数组中最小的两个结点取出,求和后推进数组中);
I --> J(更新父结点和左右孩子);
J --> K(将新结点存储到out数组中);
K --> L(对node数组进行冒泡排序);
L --> M(重复执行I到L的步骤直到node数组中只剩一个结点);
M --> N(将最后剩余的结点作为根结点);
N --> O(构建树结构);
O --> P(调用creatCode函数,通过中序遍历构建编码);
P --> Q(输出各结点的信息);
Q --> R(结束);
```
#pragma once #include <assert.h> namespace mwq { template <class T> struct Less { bool operator()(const T& x, const T& y) const { return x < y; } }; template <class T> struct Greater { bool operator()(const T& x, const T& y) const { return x > y; } }; template <class T, class Container = vector<T>, class Compare = Less<T>> //默认是大堆 class priority_queue { void Adjustup(int child) { int parent = (child - 1) / 2; Compare com; while (child > 0) { //if (_con[parent] < _con[child]) if(com(_con[parent], _con[child])) { swap(_con[parent], _con[child]); child = parent; parent = (child - 1) / 2; } else { break; } } } void AdjustDown(int parent) { int child = parent * 2 + 1; Compare com; while (child < _con.size()) { //if (child + 1 < _con.size() && _con[child] < _con[child + 1]) if (child + 1 < _con.size() && com(_con[child], _con[child + 1])) { child++; } //if (_con[parent] < _con[child]) if (com(_con[parent], _con[child])) { swap(_con[parent], _con[child]); parent = child; child = parent * 2 + 1; } else { break; } } } public: priority_queue() :_con() {} void push(const T& x) { _con.push_back(x); Adjustup(_con.size() - 1); } void pop() { assert(!empty()); swap(_con[0], _con[_con.size() - 1]); _con.pop_back(); AdjustDown(0); } T& top() { assert(!empty()); return _con[0]; } int size() const { return _con.size(); } bool empty() const { return _con.size() == 0; } private: vector<T> _con; }; void test1() { priority_queue<int> pq; pq.push(1); pq.push(2); pq.push(3); pq.push(4); pq.push(5); pq.push(6); while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); } cout << endl; } }
根据错误信息来看,你在编译 `void mwq::priority_queue<int,std::vector<T>,mwq::Less<T>>::AdjustDown(int)` 这个类模板成员函数时出现了问题。根据代码来看,你在类模板 `priority_queue` 中定义了一个成员函数 `AdjustDown(int parent)`,但是在函数中使用了模板参数 `T`,但是没有进行模板参数的声明。建议你在函数开头增加 `template <class T>` 声明即可,像这样:
```cpp
template <class T>
void AdjustDown(int parent)
{
//...
}
```
同时,为了保证 `AdjustDown` 函数中可以使用 `Compare` 类型进行比较,你需要在函数中加上 `Compare` 类型的声明,像这样:
```cpp
template <class T, class Container = vector<T>, class Compare = Less<T>>
class priority_queue
{
public:
//...
private:
Container _con;
Compare com; // 增加 Compare 声明
};
```
希望这些修改可以解决你的问题。
阅读全文
相关推荐

















