将 STL 容器 转换为容器

2024-03-02

我正在寻找一种方法来制定具有以下内容的课程:

  • 使用具有最大“常量”的指针的 STL 容器的接口
  • 但它会在内部改变所指向的对象
  • 与非常量模拟相比,没有额外的运行时开销

理想情况下,与非常量版本相比,该解决方案不会编译为额外的代码,因为常量/非常量性只是对程序员的帮助。

到目前为止,这是我尝试过的:

#include <list>
#include <algorithm>

using namespace std;
typedef int T;

class C
{
public:
    // Elements pointed to are mutable, list is not, 'this' is not - compiles OK
    list<T *> const & get_t_list() const { return t_list_; }

    // Neither elements nor list nor' this' are mutable - doesn't compile
    list<T const *> const & get_t_list2() const { return t_list_; }

    // Sanity check: T const * is the problem - doesn't compile
    list<T const *> & get_t_list3() { return t_list_; }

    // Elements pointed to are immutable, 'this' and this->t_list_ are
    // also immutable - Compiles OK, but actually burns some CPU cycles
    list<T const *> get_t_list4() const {
        return list<T const *>( t_list_.begin() , t_list_.end() );
    }

private:
    list<T *> t_list_;
};

如果没有类型转换的解决方案,我希望获得有关如何制定具有所描述属性的类的替代建议。


让我们假设您可以转换list<T*>& to list<T const *>&。现在考虑以下代码:

list<char*> a;
list<char const*>& b = a;

b.push_back("foo");

a.front()[0] = 'x'; // oops mutating const data

这与转换是同样的概念问题T** to T const**.

如果您想提供对底层数据的只读访问,您将需要提供一些自定义视图,可能使用自定义迭代器。

像下面这样的东西。

template <typename It>
class const_const_iterator {
private:
    using underlying_value_type = typename std::iterator_traits<It>::value_type;

    static_assert(std::is_pointer<underlying_value_type>(),
                  "must be an iterator to a pointer");

    using pointerless_value_type = typename std::remove_pointer<underlying_value_type>::type;

public:
    const_const_iterator(It it) : it(it) {}

    using value_type = pointerless_value_type const*;

    value_type operator*() const {
        return *it; // *it is a T*, but we return a T const*,
                    // converted implicitly
                    // also note that it is not assignable
    }

    // rest of iterator implementation here
    // boost::iterator_facade may be of help

private:
    It it;
};

template <typename Container>
class const_const_view {
private:
    using container_iterator = typename Container::iterator;

public:
    using const_iterator = const_const_iterator<container_iterator>;
    using iterator = const_iterator;

    const_const_view(Container const& container) : container(&container) {}

    const_iterator begin() const { return iterator(container->begin()); }
    const_iterator end() const { return iterator(container->end()); }

private:
    Container const* container;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 STL 容器 转换为容器 的相关文章

随机推荐

  • 在自定义布局页面上选择图像文件并将文件复制到安装文件夹

    我的目标是让用户使用此安装程序 插入您的姓名 选择您的语言并设置自定义参数 这样就完成了 但我需要的是用户选择一个图像将其作为应用程序中的头像 这就是用户选择图像然后将文件复制到应用程序文件夹的原因 像这样的事情 这是我的代码 如果有人可以
  • Python - 雅虎财经下载所有标准普尔 500 指数股票

    我正在尝试从此处运行代码来下载所有 S P 500 股票 https pythonprogramming net sp500 company price data python programming for finance https p
  • IE10 不处理点击事件 |帮助使用 MSPointer

    请提前原谅我是一个愚蠢的菜鸟 不管怎样 我正在尝试让我的 html5 游戏在 IE10 上运行 但它没有检测到我的点击 所以我对此进行了一些研究 发现它并没有理解这意味着什么 document getElementById answer1
  • 我给 %f 的输入没有保存

    GradeOne 和 secondaryGrade 不保存我给出的数字 所以我不能得到平均值 因为 总是以 0 0 2 的除法结束 有人可以帮助我吗 include
  • 如何让 Eclipse 和 Maven 下载我需要的 JAR?

    我是 Maven 和 Eclipse 的新手 我们一直在项目中使用 Netbeans 并手动安装 jar 我以为Maven的目的是根据你的依赖关系下载所需的jar 在一个新项目中 我在我的项目下添加了以下内容dependencies我的 p
  • 如何从公共互联网访问 HTTP 端口 5001

    我有 Windows Server 2016 Data center x64 NET Core SDK 5 0 预览版 Microsoft SQL Server 2019 在服务器上 https localhost 5001 publish
  • 如何使用 NSXML 解析 Google 天气 API?

    我想使用 NSXML 解析 google 天气 API 所以请给我一些指导 This is My url http www google com ig api weather Ahemdabad 我已经采取了这样的步骤 NSURL url
  • 如何手动安装旧的 cygwin 软件包?

    我正在进行的一个项目需要旧版本的 SLAPD LDAP 服务器 并且必须在 Windows 上运行 因此我使用 cygwin 软件包 我在这里找到了我需要2 2 x的slapd版本的二进制包 http www mirrorservice o
  • 使用 igraph 绘制网络

    我想从相关矩阵创建一个网络并绘制它 我正在尝试为此使用 igraph 这是我的数据的一个子集 mydata Taxon CD1 CD2 Actinomycetaceae g Actinomyces 0 072998825 0 0313994
  • MKMapView 缩放到 viewDidLoad 上的用户位置?

    我试图在视图加载后将地图缩放到用户的当前位置 但出现错误 由于未捕获的异常 NSInvalidArgumentException 而终止应用程序 原因 无效区域 当视图加载时 有人可以帮忙吗 Cheers void viewDidLoad
  • Chrome 插件 - 上下文菜单插入文本

    我试图弄清楚如何在右键单击输入字段 可编辑 时插入预定义字符串 通常我会使用文档通过 id 获取元素 但是 由于我无法确定用户单击的元素 在任何网站上我该如何执行此操作 到目前为止我有我的清单文件 permissions contextMe
  • 如何检测 PHP JIT 是否启用

    检测 PHP 是否使用 JIT 编译并且从运行脚本启用 JIT 的最简单方法是什么 您可以通过调用直接查询opcache设置opcache get status https www php net manual en function op
  • py2exe无法从其他目录导入模块

    我将 python 源代码与 py2exe 捆绑在一起 目录结构如下 some Mod py some dir another dir some Mod py 在后者 some dir another dir some Mod py 中 我
  • 如何将Android模拟器连接回ADB?

    当我通过在 Eclipse 中点击 调试 启动模拟器后 经过一定时间后它会与 ADB 断开连接 但模拟器保持打开状态 它反应灵敏 我可以导航和启动应用程序 如何将模拟器连接回 ADB 以便能够从 Eclipse 进行调试 当前的解决方法是终
  • 允许程序通过 Windows 防火墙

    I am new to windows Firewall I have seen this dialog and unable to relate the meaning of it 上面的对话框说的是 允许 python 在这些网络上进行
  • RSS 源中的有效 HTML 标签是什么?

    我环顾四周 似乎没有任何关于 RSS 中 允许 HTML 标签的标准 你可以放入任何内容 But我测试的读者只允许某些事情 在我最初的测试中 看起来像 这样的简单样式标签就可以了 像 这样的内联样式似乎也有效 但是 和 22359 3711
  • CSS 位置元素“固定”在滚动容器内

    我想知道是否有人找到了解决方案 我正在寻找一种将元素附加到滚动容器顶部的解决方案 HTML div class container div class header title div div class element div about
  • CSS响应中心部门

    我想将一些有背景图像的 div 居中 该 div 的响应存在问题 因为如果我将宽度设置为 80 高度设置为 80 则背景图像不会位于中心 我尝试了一切 但图片不能只站在中心 如果浏览器更小或更大 这是一个非常大的问题 所以如果你看图片 我想
  • R Shiny:从 Excel 复制单元格并将其粘贴到 Shiny 应用程序中,然后使用它们创建数据表

    我正在开发一个 R Shiny 应用程序 我需要开发以下功能 我需要从 Excel 中复制单元格行 开始时一次一列 然后使用 selectizeInput textInput 或 textAreaInput 将它们粘贴到 Shiny 中 数
  • 将 STL 容器 转换为容器

    我正在寻找一种方法来制定具有以下内容的课程 使用具有最大 常量 的指针的 STL 容器的接口 但它会在内部改变所指向的对象 与非常量模拟相比 没有额外的运行时开销 理想情况下 与非常量版本相比 该解决方案不会编译为额外的代码 因为常量 非常