STL reverse iterator and its base()

本文深入解析了C++中reverse_iterator的base()函数如何将逆向迭代器转换为对应的正向迭代器,解释了其在逻辑指针与实际指针之间的差异,并通过代码实例演示了这一过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question:

I have such code :

vector <int> v;
for (int i=0; i<5; i++)
        v.push_back(i);
v.erase(find(v.rbegin(), v.rend(),2).base());

This code deletes the first element from vector v after first detected 2 (in vector remain: 0 1 2 4). What does .base() do here?


Answer:

base() converts a reverse iterator into the corresponding forward iterator. However, despite its simplicity, this correspondence is not as trivial as one might thing.

When a reverse iterator points at one element, it dereferences the previous one, so the element it physically points to and the element it logically points to are different. In the following diagram, iis a forward iterator, and ri is a reverse iterator constructed from i:

                             i, *i
                             |
    -      0     1     2     3     4     -
                       |     | 
                       *ri   ri

So if ri logically points to element 2, it physically points to element 3. Therefore, when converted to a forward iterator, the resulting iterator will point to element 3, which is the one that gets removed in your example.

The following small program demonstrates the above behavior:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main(int argc, char *argv[])
{
    std::vector<int> v { 0, 1, 2, 3, 4 };
    auto i = find(begin(v), end(v), 2);

    std::cout << *i << std::endl; // PRINTS 2

    std::reverse_iterator<decltype(i)> ri(i);
    std::cout << *ri << std::endl; // PRINTS 1
}
事实上,ri.base()总是逻辑指针的后面一个,所以这个例子里面,是2的后一个元素,3.

内容概要:本文介绍了一种利用元启发式算法(如粒子群优化,PSO)优化线性二次调节器(LQR)控制器加权矩阵的方法,专门针对复杂的四级倒立摆系统。传统的LQR控制器设计中,加权矩阵Q的选择往往依赖于经验和试错,而这种方法难以应对高维度非线性系统的复杂性。文中详细描述了如何将控制器参数优化问题转化为多维空间搜索问题,并通过MATLAB代码展示了具体实施步骤。关键点包括:构建非线性系统的动力学模型、设计适应度函数、采用对数缩放技术避免局部最优、以及通过实验验证优化效果。结果显示,相比传统方法,PSO优化后的LQR控制器不仅提高了稳定性,还显著减少了最大控制力,同时缩短了稳定时间。 适合人群:控制系统研究人员、自动化工程专业学生、从事机器人控制或高级控制算法开发的技术人员。 使用场景及目标:适用于需要精确控制高度动态和不确定性的机械系统,特别是在处理多自由度、强耦合特性的情况下。目标是通过引入智能化的参数寻优手段,改善现有控制策略的效果,降低人为干预的需求,提高系统的鲁棒性和性能。 其他说明:文章强调了在实际应用中应注意的问题,如避免过拟合、考虑硬件限制等,并提出了未来研究方向,例如探索非对角Q矩阵的可能性。此外,还分享了一些实践经验,如如何处理高频抖动现象,以及如何结合不同类型的元启发式算法以获得更好的优化结果。
在C++标准模板库(STL)中,`reverse_iterator`是一个非常有用的迭代器,它允许我们从容器的末尾开始遍历。如果你想自定义一个类似的功能,可以创建一个简单的类,模仿`std::reverse_iterator`的行为。下面是一个基本的实现示例: ```cpp template <typename T> class CustomReverseIterator { public: // 初始化,通常需要一个指向容器元素的指针和当前索引 explicit CustomReverseIterator(T* data = nullptr, size_t index = 0) : data_(data), current_index_(index) {} // 返回值类型,模拟std::reverse_iterator<T&> using iterator_category = std::random_access_iterator_tag; using value_type = T; using difference_type = ptrdiff_t; using pointer = T*; using reference = T&; // 前向移动 CustomReverseIterator& operator++() { if (current_index_ > 0) { --current_index_; } return *this; } // 后向移动 CustomReverseIterator& operator--() { if (current_index_ < data_->size()) { ++current_index_; } return *this; } // 获取指向的元素 T& operator*() const { return *(data_ - current_index_); } T* operator->() const { return &operator*(); } // 判断是否到达开头 bool operator==(const CustomReverseIterator& other) const { return data_ == other.data_ && current_index_ == other.current_index_; } bool operator!=(const CustomReverseIterator& other) const { return !(*this == other); } private: T* data_; // 存储数据的指针 size_t current_index_; // 当前索引,从末尾开始计数 }; ``` 这个自定义的`CustomReverseIterator`类假设你有一个包含元素的动态数组或其他支持随机访问的容器。你可以像操作普通迭代器一样使用它,例如: ```cpp int arr[] = {1, 2, 3, 4, 5}; CustomReverseIterator<int> it(arr, arr.size()); while (it != CustomReverseIterator<int>()) { std::cout << *it-- << " "; } // 输出:5 4 3 2 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值