反向迭代器reverse_iterator

本文介绍了一种时间基键值存储的数据结构实现,包括两个主要操作:设置和获取。通过实例展示了如何存储键值对并根据时间戳检索最近的值。

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

981. Time Based Key-Value Store

Medium

13324FavoriteShare

Create a timebased key-value store class TimeMap, that supports two operations.

1. set(string key, string value, int timestamp)

  • Stores the key and value, along with the given timestamp.

2. get(string key, int timestamp)

  • Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
  • If there are multiple such values, it returns the one with the largest timestamp_prev.
  • If there are no values, it returns the empty string ("").

 

Example 1:

Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
Output: [null,null,"bar","bar",null,"bar2","bar2"]
Explanation:   
TimeMap kv;   
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1   
kv.get("foo", 1);  // output "bar"   
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"   
kv.set("foo", "bar2", 4);   
kv.get("foo", 4); // output "bar2"   
kv.get("foo", 5); //output "bar2"   

Example 2:

Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
Output: [null,null,null,"","high","high","low","low"]
class TimeMap {
public:
    /** Initialize your data structure here. */
    TimeMap() {
        
    }
    
    void set(string key, string value, int timestamp) {
        Map[key][timestamp] = value;
    }
    
    string get(string key, int timestamp) {
        string Result = "";
        if(Map.count(key) == 0){
            return Result;
        }
        if(Map[key].count(timestamp) != 0){
            Result = Map[key][timestamp];
            return Result;
        }
        map<int,string>::reverse_iterator it = Map[key].rbegin();
        for(it;it != Map[key].rend();it++){
            if(it->first > timestamp){
                continue;
            }
            Result = it->second;
            return Result;
        }
        return Result;
    }
    private:
        unordered_map<string,map<int,string>> Map;
};

/**
 * Your TimeMap object will be instantiated and called as such:
 * TimeMap* obj = new TimeMap();
 * obj->set(key,value,timestamp);
 * string param_2 = obj->get(key,timestamp);
 */

 

### C++ 中迭代器的定义方式 在 C++ 中,迭代器是一种泛化指针的概念,用于访问容器中的元素。标准库提供了多种类型的迭代器以满足不同的需求。以下是几种常见迭代器的定义方式及其代码示例。 #### 正向迭代器 (Forward Iterator) 正向迭代器允许单向遍历序列中的元素。它支持前增操作 (`operator++`) 和解引用操作 (`*` 或 `->`) 来获取当前指向的对象[^1]。 ```cpp #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4}; // 使用正向迭代器 for (auto it = vec.begin(); it != vec.end(); ++it) { std::cout << *it << " "; // 输出: 1 2 3 4 } } ``` #### 常量正向迭代器 (Constant Forward Iterator) 常量正向迭代器不允许修改所指向对象的内容,但它可以读取这些内容[^1]。 ```cpp #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4}; // 使用 const_iterator 遍历并只读取数据 for (const auto& value : vec) { std::cout << value << " "; // 输出: 1 2 3 4 } // 显式声明 const_iterator for (std::vector<int>::const_iterator cit = vec.cbegin(); cit != vec.cend(); ++cit) { std::cout << *cit << " "; // 输出: 1 2 3 4 } } ``` #### 反向迭代器 (Reverse Iterator) 反向迭代器是从后向前遍历容器中的元素。它的行为类似于正向迭代器,只是方向相反。通常由底层实现提供适配器来转换成反向形式[^1]。 ```cpp #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4}; // 使用 reverse_iterator 遍历 for (auto rit = vec.rbegin(); rit != vec.rend(); ++rit) { std::cout << *rit << " "; // 输出: 4 3 2 1 } } ``` #### 常量反向迭代器 (Constant Reverse Iterator) 这种迭代器结合了反向特性和不可变特性,既可以从后往前移动又无法更改其指向的数据项。 ```cpp #include <iostream> #include <vector> int main() { std::vector<int> vec = {1, 2, 3, 4}; // 使用 const_reverse_iterator 遍历 for (std::vector<int>::const_reverse_iterator crit = vec.crbegin(); crit != vec.crend(); ++crit) { std::cout << *crit << " "; // 输出: 4 3 2 1 } } ``` 以上展示了如何定义以及使用不同种类的迭代器,在实际开发过程中可以根据具体场景选择合适的迭代器类型以提高程序效率和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值