恒定向量 [英] constant vector

查看:86
本文介绍了恒定向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我一直在尝试使用std :: vector。假设我想创建一个向量

的整数,但不希望向量的大小或容量发生变化。在

其他词中,它有固定数量的元素。


为此,我将4个整数的向量声明为:


const vector< int> pool(4);


现在,要设置整数值,我必须使用const_cast,如下所示:


for(int i = 0; i< pool.size(); i ++)

{

int * n = const_cast< int *>(& pool [i]);


* n = 50 + i;

}


这将使向量包含数字:50, 51,52,53。


制作向量''const''的原因是如果向量将编译器标记

错误被修改了。例如,这将产生一个

编译器错误(我希望发生此错误):


pool.push_back(9);


忽略这个简单例子的实用性,这是否有效?


-

Alvin

Hello,

I have been experimenting with std::vector. Say I wanted to create a vector
of integers but do not want the vector to change in size or capacity. In
otherwords, it has a fixed number of elements.

To do this I declared a vector of 4 integers as:

const vector<int> pool(4);

Now, to set values of the integers I had to use const_cast as in:

for(int i = 0; i < pool.size(); i++)
{
int *n = const_cast<int*>(&pool[i]);

*n = 50+i;
}

This will make the vector contain the numbers: 50, 51, 52, 53.

The reason for the making the vector ''const'' is so that a compiler will flag
an error if the vector is modified. For example, this will produce a
compiler error (I want this error to occur):

pool.push_back(9);

Ignoring the practicality of this trivial example, is this valid?

--
Alvin

推荐答案

2005年5月5日星期四18:53:07 GMT in comp.lang.c ++,Alvin

< reply@in_newsgroup.ca>写道,
On Thu, 05 May 2005 18:53:07 GMT in comp.lang.c++, Alvin
<reply@in_newsgroup.ca> wrote,
const vector< int>池(4);
.... int * n = const_cast< int *>(& pool [i]);
....忽略这个简单例子的实用性,这有效吗?
const vector<int> pool(4); .... int *n = const_cast<int*>(&pool[i]); ....Ignoring the practicality of this trivial example, is this valid?




编号当你声明向量const时,编译器可以自由

以这样的方式实现它,即在构造之后修改它

可能不起作用。


使用其中一个向量构造函数在构造时以

形式完全初始化它;或构建一个非常量向量

并创建一个const引用以供公众使用。



No. When you declare the vector const, the compiler is free to
implement it in such a way that modifying it after it is constructed
may not work.

Use one of the vector constructors that fully initializes it in the
form you want at the time of construction; or build a not-const vector
and create a const reference to it for public use.


Alvin写道:
我一直在试验std :: vector。假设我想创建一个整数向量,但不希望向量的大小或容量发生变化。在
其他词中,它具有固定数量的元素。

为此,我将4个整数的向量声明为:

const vector< int> pool(4);

现在,要设置整数的值,我必须使用const_cast,如下所示:

for(int i = 0; i< pool.size (); i ++)
{* / int * n = const_cast< int *>(& pool [i]);

* n = 50 + i;
}
这将使向量包含数字:50,51,52,53。

制作向量''const''的原因是这样的如果向量被修改,编译器将标记错误。例如,这会产生一个
编译器错误(我希望发生此错误):

pool.push_back(9);

忽略这个实用性琐碎的例子,这有效吗?
I have been experimenting with std::vector. Say I wanted to create a vector
of integers but do not want the vector to change in size or capacity. In
otherwords, it has a fixed number of elements.

To do this I declared a vector of 4 integers as:

const vector<int> pool(4);

Now, to set values of the integers I had to use const_cast as in:

for(int i = 0; i < pool.size(); i++)
{
int *n = const_cast<int*>(&pool[i]);

*n = 50+i;
}

This will make the vector contain the numbers: 50, 51, 52, 53.

The reason for the making the vector ''const'' is so that a compiler will flag
an error if the vector is modified. For example, this will produce a
compiler error (I want this error to occur):

pool.push_back(9);

Ignoring the practicality of this trivial example, is this valid?




这可能是有效的。 ''std :: vector''从免费商店

分配其内容,因此const_cast应该没有特别的问题。但是,如果我要实现一个大小恒定的向量,我可能会在我自己的界面中包含

''std :: vector''没有改变大小的操作

可用而不是声明向量''const''然后调整它

使用''const_cast''...


V



It''s probably valid. ''std::vector'' allocates its contents from free store
and thus should have no particular issue with const_cast. However, if I
were to implement a vector with constant size, I''d probably wrapped the
''std::vector'' in my own interface that would not have size-changing ops
available instead of declaring the vector ''const'' and then tweaking it
using ''const_cast''...

V


* Alvin:

我一直在尝试使用std :: vector。假设我想创建一个整数向量,但不希望向量的大小或容量发生变化。在
其他词中,它具有固定数量的元素。

为此,我将4个整数的向量声明为:

const vector< int> pool(4);

现在,要设置整数的值,我必须使用const_cast,如下所示:

for(int i = 0; i< pool.size (); i ++)
{* / int * n = const_cast< int *>(& pool [i]);

* n = 50 + i;
}
这将使向量包含数字:50,51,52,53。

制作向量''const''的原因是这样的如果向量被修改,编译器将标记错误。例如,这会产生一个
编译器错误(我希望发生此错误):

pool.push_back(9);

忽略这个实用性琐碎的例子,这是有效的吗?

I have been experimenting with std::vector. Say I wanted to create a vector
of integers but do not want the vector to change in size or capacity. In
otherwords, it has a fixed number of elements.

To do this I declared a vector of 4 integers as:

const vector<int> pool(4);

Now, to set values of the integers I had to use const_cast as in:

for(int i = 0; i < pool.size(); i++)
{
int *n = const_cast<int*>(&pool[i]);

*n = 50+i;
}

This will make the vector contain the numbers: 50, 51, 52, 53.

The reason for the making the vector ''const'' is so that a compiler will flag
an error if the vector is modified. For example, this will produce a
compiler error (I want this error to occur):

pool.push_back(9);

Ignoring the practicality of this trivial example, is this valid?




No.


你只能(正式)抛弃const而不是某些东西't const

首先。


最简单的方法是编写一个返回向量的函数

然后你可以用作初始化程序,或者使用函数或函子类

而不是向量。


更高级,你可以写一个迭代器类,用它来初始化

向量。


更高级的,你可以推广迭代器类来迭代使用

任何客户端代码函数或函数类来提供值。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



No.

You can only (formally) cast away const for something that wasn''t const
in the first place.

The simplest way to do the above is to write a function that returns a vector
which you can then use as initializer, or to use a function or functor class
instead of a vector.

More advanced, you can write an iterator class and use that to initialize the
vector.

More advanced still, you can generalize that iterator class to iterate using
any client code function or functor class to provide the values.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于恒定向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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