[英]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:
到目前为止,我已经实现了:
What I want to do next is: 我接下来要做的是:
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);
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.