向量< const T(*)>对比矢量< T(*)> [英] vector<const T(*)> vs. vector<T(*)>
问题描述
大家好,
我面临着const模板参数的一些不确定性。
也许有人可以解释一般策略。
#include< vector>
int main(int arc,char ** argv)
{
std :: vector< const intvec;
const int i = 5;
vec.push_back(i);
vec [0] = 4; // const已经消失了
std :: vector< const int * pvec;
const int * pi = new int(5);
pvec.push_back(pi);
*(pvec [0])= 4; //不可能因为const,编译错误
返回0;
}
从第一印象来看,不可能创建一个
const整数的向量。
但你可以用指针来做。
Hi all,
I''m facing some uncertainty with const template arguments.
Maybe someone could explain the general strategy.
#include <vector>
int main(int arc, char** argv)
{
std::vector<const intvec;
const int i = 5;
vec.push_back(i);
vec[0] = 4; //const has gone away
std::vector<const int*pvec;
const int* pi = new int(5);
pvec.push_back(pi);
*(pvec[0]) = 4; // not possible because const, compile error
return 0;
}
From the first impression, it is not possible to create a vector of
const ints.
But you can do it with pointers.
推荐答案
On 30 okt,12:04,eiji.anonrem ... @ googlemail.com写道:
On 30 okt, 12:04, eiji.anonrem...@googlemail.com wrote:
大家好,
我面临一些const模板参数的不确定性。
也许有人可以解释一般策略。
#include < vector>
int main(int arc,char ** argv)
{
std :: vector< const intvec;
const int i = 5;
vec.push_back(i);
vec [0] = 4; // const已经消失了
std :: vector< const int * pvec;
const int * pi = new int(5);
pvec.push_back(pi);
*(pvec [0])= 4; //不可能因为const,编译错误
返回0;
}
来自第一印象,不可能创建一个
const int的向量。
但你可以用指针来做。
Hi all,
I''m facing some uncertainty with const template arguments.
Maybe someone could explain the general strategy.
#include <vector>
int main(int arc, char** argv)
{
std::vector<const intvec;
const int i = 5;
vec.push_back(i);
vec[0] = 4; //const has gone away
std::vector<const int*pvec;
const int* pi = new int(5);
pvec.push_back(pi);
*(pvec[0]) = 4; // not possible because const, compile error
return 0;
}
From the first impression, it is not possible to create a vector of
const ints.
But you can do it with pointers.
当你把任何你放入向量的东西时,你得到它的副本。
这意味着你的副本不是常量,无论你输入什么。
你输入的指针不是const - 它们指向的是int。一个指向可变int的
const指针看起来像
int * const x;
When you take whatever you put into the vector, you get a copy of it.
That means that your copy is not const, no matter what you put in.
The pointers you put in are not const - the ints they point to are. A
const pointer to a changeable int looks like
int * const x;
当你把任何你放入向量的东西时,你得到它的副本。
这意味着你的副本不是const,不是无论你投入什么。
When you take whatever you put into the vector, you get a copy of it.
That means that your copy is not const, no matter what you put in.
#include< vector>
模板< class Tclass MyInt {
T var;
public:
MyInt(T i):var(i){}
T GetT(){return var;}
T& GetTRef(){return var;}
MyInt& operator =(const MyInt& t){
if(this!=& t)
{
var = t.GetT() ;
}
返回* this;
}
};
int main(int arc,char ** argv)
{
const int i = 5;
MyInt< ; const intmi(i);
//mi.GetTRef()= 4; //错误,变量是const
int x = 5;
MyInt< intmx(x);
mx.GetTRef()= 4 ; //这个有效
返回0;
}
这真的是理由吗?在该示例中,您可以使用const int实例化
MyInt。并且只有int。在这两种情况下,值都是复制的,但var不是int。在这两种情况下。
#include <vector>
template<class Tclass MyInt {
T var;
public:
MyInt(T i):var(i){}
T GetT() {return var;}
T& GetTRef() {return var;}
MyInt& operator=(const MyInt& t) {
if (this != &t)
{
var = t.GetT();
}
return *this;
}
};
int main(int arc, char** argv)
{
const int i = 5;
MyInt<const intmi(i);
//mi.GetTRef() = 4; // error, variable is const
int x = 5;
MyInt<intmx(x);
mx.GetTRef() = 4; // this works
return 0;
}
Is that really a justification? In that example you can instantiate
MyInt with "const int" and only "int". In both cases the value is
copied, but var is not "int" in both cases.
10月30日,6:04 * am,eiji.anonrem ... @ googlemail.com写道:
On Oct 30, 6:04*am, eiji.anonrem...@googlemail.com wrote:
大家好,
我面临一些const模板参数的不确定性。
也许有人可以解释一般策略。
#include< vector>
int main(int arc,char ** argv)
{
* * * * std :: vector< const intvec;
* * * * const int i = 5;
* * * * vec.push_back(i);
* * * * vec [0] = 4; // const已经消失了
* * * * std :: vector< const int * pvec;
* * * * const int * pi = new int(5);
* * * * pvec.push_back(pi);
* * * * *(pvec [0])= 4; //不可能因为const,编译错误
* * * *返回0;
}
从第一印象开始,就不可能创建一个
const整数的向量。
但你可以用指针来做。
Hi all,
I''m facing some uncertainty with const template arguments.
Maybe someone could explain the general strategy.
#include <vector>
int main(int arc, char** argv)
{
* * * * std::vector<const intvec;
* * * * const int i = 5;
* * * * vec.push_back(i);
* * * * vec[0] = 4; //const has gone away
* * * * std::vector<const int*pvec;
* * * * const int* pi = new int(5);
* * * * pvec.push_back(pi);
* * * * *(pvec[0]) = 4; // not possible because const, compile error
* * * * return 0;
}
From the first impression, it is not possible to create a vector of
const ints.
But you can do it with pointers.
像
std :: vector<>这样的容器中元素的要求之一是这些元素是可分配的和可复制的。所以
const int是一个no-no但是const int *很好,因为那个指针可以是
重新安装。
One of the requirements for elements in a container like
std::vector<>, is that those elements be assigneable and copyable. So
const int is a no-no but const int* is fine since that pointer can be
reseated.
这篇关于向量< const T(*)>对比矢量< T(*)>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!