std :: vector - bug还是功能? [英] std::vector - bug or feature?

查看:54
本文介绍了std :: vector - bug还是功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在为可变数量的玩家开发多人游戏,而在客户端,我正在使用std :: vector< CPlayer>存储

有关玩家的信息。 CPlayer是一个包含另一个std :: vector< CPosition>的类。因为其中一个玩家本身就是

客户端(并且游戏的大小< CPlayer>在游戏过程中没有变化),我想我可以存储一个

std :: vector< CPlayer> :: iterator" localplayer"指向向量的相应元素。

奇怪的是,当我向std :: vector< CPosition>添加元素时(这只不过是CPlayer的常用成员)

迭代器localplayer变得某种程度上无效(以某种方式,它指向的内存不再是实际的本地播放器,而是内存中的某个随机位置)。这有什么不对?我没有改变任何关于std :: vector< CPlayer>那么为什么我不能使用本地播放器来支付b $ b迭代器了?并且:它是否有助于存储对元素的引用而不是迭代器?我通过使用索引代替迭代器修复了这个问题,但是我真的很好奇。


提前感谢

janina

hi ng,

i''m working on a multiplayer game for a variable number of players and on the client side, i''m using a std::vector<CPlayer> to store
informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the
client itself (and the size of the vector<CPlayer> doesn''t change during a game), i thought i could store a
std::vector<CPlayer>::iterator "localplayer" that points to the respective element of the vector.
The strange thing is that when i add elements to the std::vector<CPosition> (which is nothing more than a usual member of CPlayer)
the iterator "localplayer" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplayer" but
rather some random position in memory). what''s wrong here? i didn''t change anything about the std::vector<CPlayer> so why can''t i
use the "localplayer" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
this problem by using an index instead of iterator, but i''m really curious about this.

thanks in advance
janina

推荐答案

On Sun,2004年4月11日16:12:40 +0200," Janina Kramer"

< j。****** @ school-sucks.com>写道:
On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
<j.******@school-sucks.com> wrote:


我正在为可变数量的玩家和客户端的多人游戏工作,我是使用std :: vector< CPlayer>存储有关玩家的信息。 CPlayer是一个包含另一个std :: vector< CPosition>的类。因为其中一个玩家本身就是
客户端(并且游戏的大小< CPlayer>在游戏中没有变化),我想我可以存储一个
std :: vector< CPlayer> :: iterator" localplayer"指向向量的相应元素。
奇怪的是,当我向std :: vector< CPosition>添加元素时(它只不过是CPlayer的常用成员)
迭代器localplayer变得某种程度上无效(以某种方式,它所指向的记忆不再是实际的本地玩家,而是在内存中的某个随机位置)。这有什么不对?我没有改变任何关于std :: vector< CPlayer>那么为什么我不能使用本地播放器呢?迭代器了?并且:它是否有助于存储对元素的引用而不是迭代器?我通过使用索引而不是迭代器来解决这个问题,但我真的很好奇。


手写,你所描述的似乎不应该是单独迭代所有的无效的b / b
。也许迭代器只是被程序中的一些错误弄坏了(例如,使用越界向量赋值

索引)?

-leor

提前致谢
janina
hi ng,

i''m working on a multiplayer game for a variable number of players and on the client side, i''m using a std::vector<CPlayer> to store
informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the
client itself (and the size of the vector<CPlayer> doesn''t change during a game), i thought i could store a
std::vector<CPlayer>::iterator "localplayer" that points to the respective element of the vector.
The strange thing is that when i add elements to the std::vector<CPosition> (which is nothing more than a usual member of CPlayer)
the iterator "localplayer" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplayer" but
rather some random position in memory). what''s wrong here? i didn''t change anything about the std::vector<CPlayer> so why can''t i
use the "localplayer" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
this problem by using an index instead of iterator, but i''m really curious about this.
Offhand, what you''ve described doesn''t seem like it should be invalidating
that iterator all by itself. Perhaps the iterator is just getting corrupted
by some bug in your program (e.g., assignment using an out-of-bounds vector
index)?
-leor

thanks in advance
janina




-

Leor Zolman - - BD软件--- www.bdsoft.com

开 - C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


On Sun,2004年4月11日16:12:40 +0200,Janina Kramer

< j。****** @ school-sucks.com>写道:
On Sun, 11 Apr 2004 16:12:40 +0200, "Janina Kramer"
<j.******@school-sucks.com> wrote:


我正在为可变数量的玩家和客户端的多人游戏工作,我是使用std :: vector< CPlayer>存储有关玩家的信息。 CPlayer是一个包含另一个std :: vector< CPosition>的类。因为其中一个玩家本身就是
客户端(并且游戏的大小< CPlayer>在游戏中没有变化),我想我可以存储一个
std :: vector< CPlayer> :: iterator" localplayer"指向向量的相应元素。
奇怪的是,当我向std :: vector< CPosition>添加元素时(它只不过是CPlayer的常用成员)
迭代器localplayer变得某种程度上无效(以某种方式,它所指向的记忆不再是实际的本地玩家,而是在内存中的某个随机位置)。这有什么不对?我没有改变任何关于std :: vector< CPlayer>那么为什么我不能使用本地播放器呢?迭代器了?并且:它是否有助于存储对元素的引用而不是迭代器?我通过使用索引而不是迭代器来解决这个问题,但我真的很好奇。


手写,你所描述的似乎不应该是单独迭代所有的无效的b / b
。也许迭代器只是被程序中的一些错误弄坏了(例如,使用越界向量赋值

索引)?

-leor

提前致谢
janina
hi ng,

i''m working on a multiplayer game for a variable number of players and on the client side, i''m using a std::vector<CPlayer> to store
informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the
client itself (and the size of the vector<CPlayer> doesn''t change during a game), i thought i could store a
std::vector<CPlayer>::iterator "localplayer" that points to the respective element of the vector.
The strange thing is that when i add elements to the std::vector<CPosition> (which is nothing more than a usual member of CPlayer)
the iterator "localplayer" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplayer" but
rather some random position in memory). what''s wrong here? i didn''t change anything about the std::vector<CPlayer> so why can''t i
use the "localplayer" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed
this problem by using an index instead of iterator, but i''m really curious about this.
Offhand, what you''ve described doesn''t seem like it should be invalidating
that iterator all by itself. Perhaps the iterator is just getting corrupted
by some bug in your program (e.g., assignment using an out-of-bounds vector
index)?
-leor

thanks in advance
janina




-

Leor Zolman - - BD软件--- www.bdsoft.com

开 - C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html




" Janina Kramer" < J。****** @ school-sucks.com>在消息中写道

news:c5 ************* @ news.t-online.com ...

"Janina Kramer" <j.******@school-sucks.com> wrote in message
news:c5*************@news.t-online.com...
hi ng,

我正在为可变数量的玩家开发多人游戏,而在客户端的
,我正在使用std :: vector< CPlayer>存储关于球员的信息。 CPlayer是一个包含另一个
std :: vector< CPosition>的类。因为其中一个玩家本身就是客户端(并且在
游戏期间向量< CPlayer>的大小没有变化),我想我可以存储一个std :: vector< CPlayer> :: iterator" ; localplayer"指向向量的相应
元素。奇怪的是,当我向
std :: vector< CPosition>添加元素时(它只不过是一个普通的成员)$ it
CPlayer)迭代器localplayer变得某种程度上无效(以某种方式,它指向的
内存不再是实际的本地播放器,而是内存中的一些随机位置)。这有什么不对?我没有改变
关于std :: vector< CPlayer>的任何内容。那么为什么我不能使用本地播放器呢?迭代器了?并且:它是否有助于存储
引用元素而不是迭代器?我通过使用索引代替迭代器来解决这个问题,但我真的很好奇
关于此。
提前感谢
janina
hi ng,

i''m working on a multiplayer game for a variable number of players and on the client side, i''m using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn''t change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the respective element of the vector. The strange thing is that when i add elements to the std::vector<CPosition> (which is nothing more than a usual member of
CPlayer) the iterator "localplayer" becomes somehow invalid (in a way that the memory it points to is no longer the actual "localplayer" but rather some random position in memory). what''s wrong here? i didn''t change anything about the std::vector<CPlayer> so why can''t i use the "localplayer" iterator any more? and: would it help to store a reference to the element rather than an iterator? i fixed this problem by using an index instead of iterator, but i''m really curious about this.
thanks in advance
janina




我害怕你必须搞砸了某个地方因为你说的话

听起来不错而且

我愿意赌很多钱没有像你在std :: vector中描述的那样的bug。


你确定CPlayer矢量的大小不是吗?改变?

你如何创造它以及如何添加玩家?

唯一合理的方式来做你所说的:


std :: vector< CPlayer>玩家;

players.reserve(MAX_PLAYERS);

最多MAX_PLAYERS

players.push_back(玩家);





std :: vector< CPlayer>玩家(MAX_PLAYERS);
每位玩家


设置(玩家[i]);


后者可能会相当丑陋。



I''m afraid that you must have screwed up somewhere because what you say
sounds OK and
I''m willing to bet a lot of money that there is no bug like that which you
describe in std::vector.

Are you sure that the size of the CPlayer vector doesn''t change?
How do you create it and how do you add players?
The only reasonables way to do what you say are:

std::vector<CPlayer> players;
players.reserve(MAX_PLAYERS);
for up to MAX_PLAYERS
players.push_back(player);

or

std::vector<CPlayer> players(MAX_PLAYERS);
for each player
setup(players[i]);

The latter would probably be quite ugly.


这篇关于std :: vector - bug还是功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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