- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我经常使用值对:std::pair<int, int> my_pair
.有时我需要对 my_pair.first
执行相同的操作和 my_pair.second
.
如果我能做到 my_pair[j]
我的代码会更流畅并遍历 j=0,1。(我避免使用数组,因为我不想为分配内存而烦恼,并且我将 pair
广泛用于其他事情)。
因此,我想定义 operator[]
对于 std::pair<int, int>
.
而且我无法让它工作,(我不太擅长使用模板等)...
#include <utility>
#include <stdlib.h>
template <class T1> T1& std::pair<T1, T1>::operator[](const uint &indx) const
{
if (indx == 0)
return first;
else
return second;
};
int main()
{
// ....
return 0;
}
编译失败。其他变体也会失败。
据我所知,我正在关注 Stack Overflow operator overloading FAQ ,但我想我遗漏了一些东西......
最佳答案
这是一个非成员实现:
/// @return the nth element in the pair. n must be 0 or 1.
template <class T>
const T& pair_at(const std::pair<T, T>& p, unsigned int n)
{
assert(n == 0 || n == 1 && "Pair index must be 0 or 1!");
return n == 0 ? p.first: p.second;
}
/// @return the nth element in the pair. n must be 0 or 1.
template <class T>
T& pair_at(std::pair<T, T>& p, unsigned int index)
{
assert(index == 0 || index == 1 && "Pair index must be 0 or 1!");
return index == 0 ? p.first: p.second;
}
// usage:
pair<int, int> my_pair(1, 2);
for (int j=0; j < 2; ++j)
++pair_at(my_pair, j);
请注意,我们需要两个版本:一个用于只读对,一个用于可变对。
不要害怕随意使用非成员函数。正如 Stroustrup 自己所说,没有必要用一个对象来模拟一切,也没有必要通过继承来扩充一切。如果您确实想使用类,请选择组合而不是继承。
你也可以这样做:
/// Applies func to p.first and p.second.
template <class T, class Func>
void for_each_pair(const std::pair<T, T>& p, Func func)
{
func(p.first);
func(p.second);
}
/// Applies func to p.first and p.second.
template <class T, class Func>
void for_each_pair(std::pair<T, T>& p, Func func)
{
func(p.first);
func(p.second);
}
// usage:
pair<int, int> my_pair(1, 2);
for_each_pair(my_pair, [&](int& x){
++x;
});
如果您有 C++11 lambda 表达式,那么使用它并不会太笨拙,而且至少更安全一些,因为它不可能越界访问。
关于c++ 为 std::pair 重载 operator[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9526430/
我正在学习使用 STL 的排序函数,方法是将它用于一些复杂的对 vector 。 我有以下 vector : vector > > > > 我需要先根据对中的第一个整数对元素进行排序,如果发现有 2
我想初始化: pair>,vector>> pvp; 所以对于所有的我: pvp.first[i].first = true; 和 pvp.second[i].first = false; 我知道您可
那为什么我们能实例化Pair却不能实例化Pair Pair p=new Pair(); 对比 Pair p=new Pair(); 我知道 意思是未知类型 --> 但不是 意思是一样的---> 有
这个问题在这里已经有了答案: How to have an unordered_map where the value type is the class it's in? (1 个回答) `std
我正在编写一个代码来处理warehouse[item[batch, qty]]的组合,然后将基于[batch, qty]的batch与qty的总和分组。我的代码是: package main impo
我想知道最好的类 java 容器是什么 > 我有一个作为键的 object1 以及一个 t1 和 t2。 我的类具有以下属性: public class Patient implements Exte
所以我有一些使用 Java 8 流的代码,而且它可以工作。它做的正是我需要它做的,而且清晰易读(这在函数式编程中很少见)。在子例程结束时,代码遍历自定义对类型的列表: // All names Hun
我正在声明一个字符串映射到一对对,如下所示: std::map, std::pair>> reference; 我将其初始化为: reference.insert
例如,镜像 pair 至 pair ,我可以像这样创建一个模板函数: template void mirror(const AB& ab,BA& ba){ ba.first=ab.secon
我正在使用 PyZMQ 创建一个简单的 PAIR/PAIR 通信原型(prototype)消息传递模式。 配对服务器 import zmq import random import sys impor
我正在尝试习惯 Kotlin 中的习语和快捷方式,我想知道是否有任何方法可以做到这一点。 val pairList = listOf(Pair(1, 2), Pair(5, 10), Pair(12,
我定义的 map 是这样的 map > hmap; 如果有一个pair(2,pair(3,4))如何得到2 3 4个值,itr->first, itr->第二个不工作 最佳答案 If there is
我希望能够对以下 vector 进行排序 -vector>> 基于 pair 的第一个元素,如果它们相等,则根据它们的第二个元素对它们进行排序,我如何使用 STL 在 C++ 中做到这一点构建? 这种
通过 PHP_PDO: fetchAssoc echo 从 MySql SELECT 查询得到一个 $.getJSON 结果使用 json_encode() 编辑,在 firebug 控制台中的输出如
假设我有以下功能: (defun f (v1 v2) ...) 我想简化以下代码: (lambda (pair) (apply #'f pair)) 此处的目标是创建一个函数,该函数接受两个值的列表并
对不起,我真的不知道怎么写这个标题。我可以想到这样做的代价高昂的方法,但我想看看是否有人可以指出一个优雅的解决方案,这里是: 我有很多成对关联的元素; 每个元素都有一个与其自身关联的数值,该数值表示它
我有一个使用以下方法用 Java 编写的 PreferencesManager: public void insert(Pair keyValue, boolean async) { 我正尝试从 k
这是 assigning-of-unordered-map-to-pair-of-objects 的后续问题.这是一个关于编译器错误解释的问题(而不是一个重复的问题,因为该问题已经得到了完整的回答)。
这是我的 map : map, pair > matchMap; 这是函数: void Schedule::studentSchedule() { string s, c; cout
#include #include #include using namespace std; int main() { map,pair> items; items.inser
我是一名优秀的程序员,十分优秀!