vector ::迭代器到指针转换 [英] vector::iterator to a pointer casting

查看:1765
本文介绍了vector ::迭代器到指针转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么编译器无法隐式地将向量<> :: iterator

转换为void *?


我有:

void f(void *,size_t);

....

vector< int> v;

v.reserve(n);

f(v.begin(),n * sizeof(int));


和编译器(VC7.1)告诉我它无法将v.begin()转换为

void *。最简单的方法是什么? &(* v.begin())?

Why it''s impossible for compiler to implicitly cast vector<>::iterator
to void*?

I have:
void f(void *, size_t);
....
vector<int> v;
v.reserve(n);
f(v.begin(), n * sizeof(int));

And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What''s the easiest way to do it? &(*v.begin())?

推荐答案




Raider写道:
Hi

Raider wrote:
为什么编译器无法隐式地将向量<> :: iterator
转换为void *?


因为vector< T> :: iterator _may_是T *,但不一定是。

编译器(VC7.1)告诉我它无法将v.begin()转换为
void *。最简单的方法是什么? &(* v.begin())?
Why it''s impossible for compiler to implicitly cast vector<>::iterator
to void*?
Because vector<T>::iterator _may_ be a T*, but need not be.
And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What''s the easiest way to do it? &(*v.begin())?




它会起作用(除非v.size()== 0,ievbegin()== v.end ()),但我认为

使用& v [0]更容易也更常见。


Markus



It would work (unless v.size() == 0, i.e. v.begin() == v.end()), but I think
it''s easier and more common to use &v[0].

Markus


Raider写道:
为什么编译器无法隐式地将向量<> :: iterator
转换为void *?

我有:
void f(void *,size_t);
...
vector< int> v;
v.reserve(n);
f(v.begin(),n * sizeof(int));

编译器(VC7.1)告诉我它无法将v.begin()转换为
void *。最简单的方法是什么? &(* v.begin())?
Why it''s impossible for compiler to implicitly cast vector<>::iterator
to void*?

I have:
void f(void *, size_t);
...
vector<int> v;
v.reserve(n);
f(v.begin(), n * sizeof(int));

And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What''s the easiest way to do it? &(*v.begin())?




您会发现C ++通常不允许您隐式地使用
关于任何类型的无效*,期间。


但是如果你坚持,你可以做以下相当丑陋的代码:


void * pt = static_cast< void *>(& v [0]);


问候,

Ben



You will find out that C++ generally does not allow you to implicitly
cast just about any type to void*, period.

But if you insist, you can do it the following rather ugly code:

void* pt = static_cast<void*>(&v[0]);

Regards,
Ben


Raider写道:
为什么编译器无法隐式地将向量<> :: iterator
转换为void *?


因为vector<> :: iterator不是指针。


那就像问为什么它不能转换向量到void指针。


迭代器有指针语法和指针语义,但是

并不代表​​它是一个指针。

我有:
void f(void *,size_t);
...
vector< int> v;
v.reserve(n);
f(v.begin(),n * sizeof(int));

编译器(VC7.1)告诉我它无法将v.begin()转换为
void *。最简单的方法是什么? &(* v.begin())?
Why it''s impossible for compiler to implicitly cast vector<>::iterator
to void*?
because vector<>::iterator is not a pointer.

That''s like asking why it can''t convert the vector to a void pointer.

The iterator has pointer syntax, and much of pointer semantics, but that
doesn''t mean it IS a pointer.
I have:
void f(void *, size_t);
...
vector<int> v;
v.reserve(n);
f(v.begin(), n * sizeof(int));

And compiler (VC7.1) tells me that it unable to convert v.begin() to
void*. What''s the easiest way to do it? &(*v.begin())?




是的。


Ben Pope

-

我不仅仅是一个数字。对很多人来说,我被称为字符串...



Yeah.

Ben Pope
--
I''m not just a number. To many, I''m known as a string...


这篇关于vector ::迭代器到指针转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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