我正在编写一个函数,它需要一只手并检查对:
int containsPairs(vector<Card> hand)
{
int pairs{ 0 };
loopstart:
for (int i = 0; i < hand.size(); i++)
{
Card c1 = hand[i];
for (int j = i + 1; j < hand.size(); j++)
{
Card c2 = hand[j];
if (c1.getFace() == c2.getFace())
{
pairs++;
hand.erase(hand.begin() + i);
hand.erase(hand.begin() + (j - 1));
goto loopstart;
}
}
}
return pairs;
}
Run Code Online (Sandbox Code Playgroud)
当它在第10行找到对时,我想删除它找到该对的手牌,然后用已删除的卡重新启动整个循环以找到第二对,如果有的话.对我来说,goto是最直观的方式,但在这种情况下,是真的吗?
我试图弄清楚如何访问 NPM 模块中定义的类型。
\nTinyMCE 有一个名为 React 组件的编辑器Editor,它包含一个onInit带有两个参数的方法,一个事件和编辑器本身,也是一种Editor类型,但与组件不同。
所以我Editor通过执行以下操作从库中导入组件
import { Editor as TinyMCEEditor } from \'@tinymce/tinymce-react\';\nRun Code Online (Sandbox Code Playgroud)\n我将编辑器定义如下......
\n<TinyMCEEditor\n apiKey="asdf"\n onInit={(evt, editor) => onInitCallBack(evt, editor)}\n ...\n/>\nRun Code Online (Sandbox Code Playgroud)\n然后我尝试定义回调以匹配预期类型
\nconst onInitCallBack = (_, editor: Editor) => {\n ...\n};\nRun Code Online (Sandbox Code Playgroud)\nEditor奇怪的是,当鼠标悬停在上面时,它能够识别类型。
(parameter) editor: Editor\nRun Code Online (Sandbox Code Playgroud)\n以及事件的类型:
\n(parameter) evt: {\n readonly type: string;\n readonly target: any;\n readonly isDefaultPrevented: () => boolean;\n readonly preventDefault: () => void;\n ...etc\n}\n …Run Code Online (Sandbox Code Playgroud) 我正在编写一个大富翁游戏.而且我真的很喜欢用于制作漂亮干净输出的排序功能,在这种情况下,按照组对玩家拥有的属性进行排序.
首先,这是我的属性分类器功能
bool propertySort(Property a, Property b) { return a.getGroup() < b.getGroup(); }
Run Code Online (Sandbox Code Playgroud)
现在,玩家拥有这些属性的列表.
#pragma once
#include <string>
#include <vector>
#include "Dice.h"
#include "Property.h"
class Player
{
public:
...
private:
int curMoney{ 1500 };
int curLocation{ 0 };
int playerNum{ 0 };
std::string name;
std::vector<Property> ownedProperties{};
int curDiceRoll;
};
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,它是一个底层的私有属性,它被归为0属性.
因此,在我的主要物业分拣机中,我会在棋盘上显示属性以供玩家查看,分拣机功能不断给我一个错误C2280.我知道正是这种排序函数抛出了这个错误,因为如果我注释掉排序行,程序运行正常.我确信这是显而易见的,因为我是初学程序员,所以我一无所知.如果有人能够提供有关什么是伟大的见解,谢谢!
void viewProperties(Player& p)
{
string prompt = "Select a player to view their properties";
vector<string> playerNames{};
for (Player p : players)
{
playerNames.push_back(p.getName());
}
Menu propertiesMenuSelection{ prompt, playerNames }; …Run Code Online (Sandbox Code Playgroud) 假设我们有一个 [2, 5, 7, 9, 3] 值的数组,我希望 2 是 100,因为它是最低值,而 9 是 0,因为它是最高值,并且中间的所有内容都是插值的,我将如何转换这个数组?当我说插值时,我希望数字在新比例中相距相同的距离,因此 3 不会完全是 100,而是接近,可能在 95 左右。