简体   繁体   English

我应该使用哪种数据类型?

[英]Which datatype should I use?

I'm really new to C++ and I'm having a confusion over something. 我真的是C ++的新手,我对某些东西感到困惑。 What I've achieved so far is: 到目前为止,我已经实现了:

  • Created a class named CPlayer 创建一个名为CPlayer的类

What I want to do next is: 我接下来要做的是:

  • Create a datatype (array/map/or whatever) to store the CPlayer in for each individual player. 创建一个数据类型(数组/地图/或其他)来为每个播放器存储CPlayer。 (PS: Each player can have an entirely different ID from the other player, for example: player1 has ID 1, player2 has ID 5) (PS:每个玩家可以具有与另一个玩家完全不同的ID,例如:玩家1的ID为1,玩家2的ID为5)

In Squirrel I could do: 在松鼠中,我可以做:

local playerInfo = {}; // Create a table
playerInfo[ playerId ]      <-  CPlayer();
// To remove it -
playerInfo.rawdelete( playerId );

Not sure what would be best in C++ to reproduce this. 不知道在C ++中最好的方式来重现此内容。

Having just looked up what a table is in Squirrel, and from reading the question above it seems the C++ version of what you want is 刚刚查询了Squirrel中的表之后,通过阅读上面的问题,看来您想要的是C ++版本

#include <map>

std::map<int, CPlayer> playerInfo;

The equivalent of playerInfo[ playerId ] <- CPlayer(); 相当于playerInfo[ playerId ] <- CPlayer(); would be 将会

playerInfo[playerId] = CPlayer();

and the equivalent of playerInfo.rawdelete( playerId ); 以及等效的playerInfo.rawdelete( playerId ); would be 将会

playerInfo.erase(playerId);

More information here 更多信息在这里

You can use std::map as shown below. 您可以使用std :: map,如下所示。 Below sample should give fair idea on the usage. 下面的示例应在用法上给出合理的思路。

std::map<into, PlayersInfo> mapPlayerInfo:
int nPlayerId1 = 1; // Player 1 Id sample
int nPlayerId2 = 2; // Player 2 Id sample

PlayerInfo player1(nPlayerId1,...); // Additional arguments to constructor if required
mapPlayerInfo[nPlayerId1] = player1;

PlayerInfo player2(nPlayerId2,...); // Sample 2
mapPlayerInfo[nPlayerId2] = player2;

//Deleting based on player Id
mapPlayerInfo.erase(nPlayerId1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM