Can I use const in vectors to allow adding elements, but not modifications to the already added?(我可以在向量中使用 const 来允许添加元素,但不能修改已添加的元素吗?)
问题描述
我对 this answer 的评论让我开始思考这些问题常量和排序.我玩了一下,将我的问题减少到以下代码:
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 int 的向量.显然,我应该知道这一点(并且在智力上我做到了),但我以前从来不需要创造这样的东西.但是,它对我来说似乎是一个有用的构造,我想知道是否有任何方法可以解决这个问题 - 我想将东西添加到向量(或其他),但一旦添加就不应更改它们.强>
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.
可能有一些令人尴尬的简单解决方案,但这是我以前从未考虑过的.我可能不应该提到排序(我可能会问另一个问题,请参阅 this 用于提出问题的困难).我真正的基本用例是这样的:
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)的要求,以及Assignable类型的附加要求.
The type of objects stored in these components must meet the requirements of
CopyConstructibletypes (20.1.3), and the additional requirements ofAssignabletypes.
这是目前阻止您使用 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 为 Destructible 和对 T 的附加要求是按表达式指定的,例如std::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 来允许添加元素,但不能修改已添加的元素吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我可以在向量中使用 const 来允许添加元素,但不能修改已添加的元素吗?
- 将函数的返回值分配给引用 C++? 2022-01-01
- 使用 __stdcall & 调用 DLLVS2013 中的 GetProcAddress() 2021-01-01
- GDB 不显示函数名 2022-01-01
- 将 hdc 内容复制到位图 2022-09-04
- OpenGL 对象的 RAII 包装器 2021-01-01
- XML Schema 到 C++ 类 2022-01-01
- 从父 CMakeLists.txt 覆盖 CMake 中的默认选项(...)值 2021-01-01
- 哪个更快:if (bool) 或 if(int)? 2022-01-01
- 如何提取 __VA_ARGS__? 2022-01-01
- DoEvents 等效于 C++? 2021-01-01
