c++ .first
时间: 2024-09-08 21:00:42 浏览: 14
`.first` 是 C++ 中 `std::pair` 类型的一个成员变量,它与 `.second` 一起用于存储一对值。`std::pair` 是标准库中的一个模板类,可以用来将两个不同类型的数据组合成一个单一的复合类型。当你创建一个 `std::pair` 实例时,你可以使用 `.first` 来访问或修改这个配对中的第一个元素。
例如:
```cpp
#include <utility> // 包含 std::pair 的头文件
#include <string>
#include <iostream>
int main() {
std::pair<std::string, int> myPair;
myPair.first = "CSDN";
myPair.second = 1999;
std::cout << "First element: " << myPair.first << std::endl;
std::cout << "Second element: " << myPair.second << std::endl;
return 0;
}
```
在这个例子中,我们定义了一个 `std::pair`,其中包含一个字符串和一个整数。`.first` 被用来设置和获取字符串部分,而 `.second` 被用来设置和获取整数部分。
相关问题
c++ .for_each
`std::for_each` 是 C++ 标准库中的一个算法函数,用于对指定范围内的元素进行操作。它接受一个可迭代的范围(如容器或迭代器对)以及一个函数对象(或函数指针),并将该函数对象应用于范围内的每个元素。
以下是 `std::for_each` 的基本语法:
```cpp
template<class InputIt, class UnaryFunction>
UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f);
```
其中:
- `InputIt` 是输入迭代器的类型,用于指定范围的开始和结束位置。
- `UnaryFunction` 是一个可调用对象类型,可以是函数对象或函数指针,用于对每个元素执行操作。
- `first` 是范围的起始位置的迭代器。
- `last` 是范围的结束位置的迭代器,指向最后一个元素的下一个位置。
- `f` 是要应用于每个元素的函数对象。
使用 `std::for_each` 可以方便地对容器中的每个元素执行操作,而无需手动编写循环。例如,我们可以使用 lambda 表达式来输出容器中的每个元素:
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
std::for_each(numbers.begin(), numbers.end(), [](int num) {
std::cout << num << ' ';
});
return 0;
}
```
上述代码将输出:`1 2 3 4 5 `,表示对容器 `numbers` 中的每个元素执行 lambda 表达式中的操作,即输出元素的值。
Least Recently used Algorithm in C++.
以下是C++中最近最少使用算法(LRU)的示例代码:
```c++
#include <iostream>
#include <unordered_map>
#include <list>
using namespace std;
class LRUCache {
private:
int capacity;
unordered_map<int, list<pair<int, int>>::iterator> cache; // 哈希表,映射key和双向链表中的迭代器
list<pair<int, int>> lruList; // 双向链表,保存 (key, value) 二元组
public:
LRUCache(int capacity) {
this->capacity = capacity;
}
int get(int key) {
if (cache.find(key) == cache.end()) {
return -1; // 如果 key 不存在,则返回 -1
}
// 把 (k, v) 提到链表头部,并更新map中该节点的迭代器
pair<int, int> kv = *cache[key];
lruList.erase(cache[key]);
lruList.push_front(kv);
cache[key] = lruList.begin();
return kv.second;
}
void put(int key, int value) {
if (cache.find(key) == cache.end()) { // 如果 key 不存在
if (lruList.size() == capacity) { // 如果缓存满了
// 删除链表尾部的键值对,并删除map中对应的项
auto lastPair = lruList.back();
int lastKey = lastPair.first;
cache.erase(lastKey);
lruList.pop_back();
}
// 在链表头部插入新的键值对,并在map中创建该项的迭代器
lruList.push_front(make_pair(key, value));
cache[key] = lruList.begin();
} else { // 如果 key 存在
// 更改链表中的节点值,并把该节点提到头部
lruList.erase(cache[key]);
lruList.push_front(make_pair(key, value));
cache[key] = lruList.begin();
}
}
};
int main() {
LRUCache cache(2); // 缓存容量为 2
cache.put(1, 1);
cache.put(2, 2);
cout << cache.get(1) << endl; // 返回 1
cache.put(3, 3); // 该操作会使得关键字 2 作废
cout << cache.get(2) << endl; // 返回 -1 (未找到)
cache.put(4, 4); // 该操作会使得关键字 1 作废
cout << cache.get(1) << endl; // 返回 -1 (未找到)
cout << cache.get(3) << endl; // 返回 3
cout << cache.get(4) << endl; // 返回 4
return 0;
}
```
LRU算法的核心是使用一个双向链表和一个哈希表,其中哈希表的键为key,值为指向双向链表中的节点的迭代器。每当访问一个节点时,将其从双向链表中删除,并将其插入到链表头部,同时更新哈希表中的值。当缓存满时,删除链表尾部的节点。当插入一个新的节点时,如果缓存已满,则删除链表尾部的节点。如果缓存未满,则直接插入到链表头部。