我可以在向量中使用const来允许添加元素,但不能对已添加的元素进行修改吗? [英] Can I use const in vectors to allow adding elements, but not modifications to the already added?

查看:106
本文介绍了我可以在向量中使用const来允许添加元素,但不能对已添加的元素进行修改吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此答案的评论让我思考了这些问题常数和排序。我试了一下,将问题归结为以下代码:

My comments on this answer got me thinking about the issues of constness and sorting. I played around a bit and reduced my issues to the fact that this code:

#include <vector>

int main() {
    std::vector <const int> v;  
}

无法编译-您无法创建const ints向量。显然,我应该知道这一点(从理智上我也知道),但是我从来不需要创建这样的东西。但是,这对我来说似乎是一个有用的结构,我想知道是否有办法解决这个问题-我想将内容添加到矢量(或其他任何内容)中,但是一旦添加它们就不应更改。

will not compile - you can't create a vector of const ints. Obviously, I should have known this (and intellectually I did), but I've never needed to create such a thing before. However, it seems like a useful construct to me, and I wonder if there is any way round this problem - I want to add things to a vector (or whatever), but they should not be changed once added.

可能有一些令人尴尬的简单解决方案,但这是我以前从未考虑过的事情。
我可能不应该提到排序(我可能会问另一个问题,请参阅,用于提出问题的难度)。我真正的基本用例是这样的:

There's probably some embarrassingly simple solution to this, but it's something I'd never considered before. I probably should not have mentioned sorting (I may ask another question about that, see this for the difficulties of asking questions). My real base use case is something like this:

vector <const int> v;     // ok (i.e. I want it to be OK)
v.push_back( 42 );        // ok
int n = v[0];             // ok
v[0] = 1;                 // not allowed


推荐答案

好,在C ++ 0x中您可以...

Well, in C++0x you can...

在C ++ 03中,有一段23.1 [lib.containers.requirements] / 3,内容为

In C++03, there is a paragraph 23.1[lib.containers.requirements]/3, which says


这些组件中存储的对象类型必须满足 CopyConstructible 类型(20.1.3)的要求,并且可分配类型的其他要求。

The type of objects stored in these components must meet the requirements of CopyConstructible types (20.1.3), and the additional requirements of Assignable types.

这是当前阻止您进行使用 const int 作为 std :: vector 的类型参数。

This is what's currently preventing you from using const int as a type argument to std::vector.

但是,在C ++ 0x中缺少此段落,相反, T 必须为 可破坏 以及 T 为每个表达式指定,例如 st =:vector 上的 v = u 仅在 T MoveConstructible MoveAssignable

However, in C++0x, this paragraph is missing, instead, T is required to be Destructible and additional requirements on T are specified per-expression, e.g. v = u on std::vector is only valid if T is MoveConstructible and MoveAssignable.

如果我正确解释了这些要求,则应该可以实例化 std :: vector< const int> ,您只会缺少它的某些功能(我想这是您想要的)。您可以通过将一对迭代器传递给构造函数来填充它。我认为 emplace_back()应该也可以工作,尽管我没有找到 T 的明确要求。

If I interpret those requirements correctly, it should be possible to instantiate std::vector<const int>, you'll just be missing some of its functionality (which I guess is what you wanted). You can fill it by passing a pair of iterators to the constructor. I think emplace_back() should work as well, though I failed to find explicit requirements on T for it.

尽管如此,您仍然无法在原位对向量进行排序。

You still won't be able to sort the vector in-place though.

这篇关于我可以在向量中使用const来允许添加元素,但不能对已添加的元素进行修改吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆