UserFile.open(cname + ".txt");
int numOfElements = name.size();
if (UserFile.is_open())
{
name.push_back(cname);
pass.push_back(cpass);
posx.push_back(0);
posy.push_back(0);
id.push_back(numOfElements);
std::cout << "Your player name has been registered successfully." << std::endl;
UserFile << cname << ";" << cpass << ";" << "0" << ";" << "0";
}
我已經得到這個了,向玩家添加玩家,甚至是numOfElements都能正常工作。我怎樣才能讀取代表玩家id的每個玩家的統計數據作爲向量中的第n個元素? 例子:向量元素ID的C++
else if (userInput == "stats") // Ignore the else
{
// Name is Allura. ID is stored too. Increments from 0 to work with the vector
// What can I do to make a way of showing information only in that nth element (like element 0 if player id is 0) etc?
}
您是否在不同的向量中存儲不同的屬性(名稱,通過,pos)?你真的應該有一個結構向量;這是C++ – qxz
你的問題有點不清楚。你想用給定的'id'找到玩家的索引嗎? – qxz
玩家1和2註冊登錄。玩家1有0存儲在ID矢量和玩家2有1作爲ID存儲。玩家1類型統計。不想顯示兩個球員的統計數據,所以我需要該程序來知道要發送哪些信息。現在,因爲玩家1在id向量中存儲了0,所以我需要它找到該玩家的ID並將其用作名稱向量中的數組索引。姓名[編號] –