C 比较共享指针的堆栈
C++ Compare stacks of shared pointers
,假设我有一个堆栈,可以为int
提供共享指针,就像以下:
#include <stack>
#include <memory>
using namespace std;
int main()
{
stack<shared_ptr<int>> s1;
stack<shared_ptr<int>> s2;
shared_ptr<int> v1 = make_shared<int>(1);
shared_ptr<int> v2 = make_shared<int>(1);
s1.push(v1);
s2.push(v2);
bool areEqual = s1 == s2; // This is false
}
如何使堆栈比较shared_ptr
指向的实际值,而不是指针本身?
std::stack
具有受保护的成员c
,它是基础容器类型的实例。您可以制作一个堆栈包装器,以访问该变量,然后比较基础容器的内容如下:
#include <iostream>
#include <stack>
#include <memory>
#include <algorithm>
using namespace std;
template<class stack_type>
struct stack_wrapper : stack_type
{
auto begin() const
{
return stack_type::c.begin();
}
auto end() const
{
return stack_type::c.end();
}
};
template<class stack_type>
const stack_wrapper<stack_type> &wrap(const stack_type &stack)
{
return static_cast<const stack_wrapper<stack_type> &>(stack);
}
int main() {
stack<shared_ptr<int>> s1;
stack<shared_ptr<int>> s2;
shared_ptr<int> v1 = make_shared<int>(1);
shared_ptr<int> v2 = make_shared<int>(1);
s1.push(v1);
s2.push(v2);
const auto &s1wrapper = wrap(s1);
const auto &s2wrapper = wrap(s2);
const auto is_equal = std::equal(s1wrapper.begin(),
s1wrapper.end(),
s2wrapper.begin(),
s2wrapper.end(),
[](auto &first, auto &second) {
return first && second && *first == *second;
});
std::cout << is_equal << std::endl;
}
我喜欢 @realfresh的答案。它准确地说明了"封装"受保护的可访问性的真实程度。但是,将基类的子对象施放到派生类的子对象引用中,然后将其视为可以迅速导致不确定的行为。
提取c
成员的想法是合理的。我们可以使用简单的实用程序而不会冒出UB的风险:
template<class S>
constexpr decltype(auto) stack_c(S&& s) {
using base = std::decay_t<S>;
struct extractor : base {
using base::c;
};
constexpr auto c_ptr = &extractor::c;
return std::forward<S>(s).*c_ptr;
}
由于表达式&extractor::c
的工作原理,我们实际上获得了指向base
(std::stack
专业化)成员的指针,名为c
。extractor
的目的是通过使用声明使该名称可公开访问。
然后,我们将其转发给它,保留的值类别以及所有内容。这是@realfresh的使用std::equal
的替换的下降:
bool areEqual = std::equal(
stack_c(s1).begin(), stack_c(s1).end(),
stack_c(s2).begin(), stack_c(s2).end(),
[](auto const& p1, auto const& p2) {
return first && second && (p1 == p2 || *p1 == *p2);
}
);
请参阅Live
相关文章:
- 从堆栈分配的原始指针构造智能指针
- 使用指针计算堆栈问题的大 O 表示法
- 作为指针注入类后重新创建堆栈对象不好吗?
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- 如何按指针查看堆栈跟踪
- 堆栈/帧指针作为外部变量
- 未分配被释放的指针(将堆栈实现为链表时)
- 唯一指针是否在堆或堆栈上分配内存?
- 了解通过引用传递取消引用指针时C++堆/堆栈分配
- C++设置堆栈指针
- 启用优化时的堆栈指针比较异常
- 从堆栈指针中查找函数参数值
- 分叉进程的零星堆栈指针分段错误
- Ostream操作员Strangley不接受我的堆栈指针
- 如何展开堆栈以获取指定堆栈指针(SP)的回溯
- 删除具有堆栈指针的派生类
- 基于整数的函数递增/递减堆栈指针
- 使用c++,我如何获得堆栈指针的值在msvc X64
- 通过创建局部变量减少堆栈指针
- 是堆栈指针,是伪随机数的良好来源