[英]Why isn't my less-than operator processing?
I just starting programming, just about a week ago, and I wanted to try and make a Blackjack game.大约一周前,我刚开始编程,我想尝试制作二十一点游戏。
I wanted to run code using a while
loop, while both hands are less than 21, run some code.我想使用
while
循环运行代码,而双手小于21,运行一些代码。
Simple enough.很简单。
But, for some reason, the less-than operator isn't working.但是,由于某种原因,小于运算符不起作用。
Here's the error message:这是错误消息:
C2676 binary '<': 'std::vector<int,std::allocator<int>>' does not define this operator or a conversion to a type acceptable to the predefined operator
C2676 二进制 '<': 'std::vector<int,std::allocator<int>>' 未定义此运算符或转换为预定义运算符可接受的类型
Can someone help me fix this?有人可以帮我解决这个问题吗?
Here's my code:这是我的代码:
#include <iostream>
#include <vector>
std::vector<int> handone(0);
std::vector<int> handtwo(0);
int main() {
while (handone < 21 && handtwo < 21) {
}
}
The error message is telling you that std::vector
does not implement an operator<
that takes an int
as input.错误消息告诉您
std::vector
没有实现将int
作为输入的operator<
。 Which is true, as it only implements anoperator<
to compare against another vector
instead.这是真的,因为它只实现了一个
operator<
来与另一个vector
进行比较。
Assuming your vector
s contain the values of individual cards, you will need to sum the values manually, such as with the standard std::accumulate()
algorithm, eg:假设您的
vector
s 包含单个卡片的值,您将需要手动对这些值求和,例如使用标准std::accumulate()
算法,例如:
#include <iostream>
#include <vector>
#include <algorithm>
std::vector<int> handone;
std::vector<int> handtwo;
int valueOfHand(const std::vector<int> &hand) {
return std::accumulate(hand.begin(), hand.end(), 0);
}
int main() {
while (valueOfHand(handone) < 21 && valueOfHand(handtwo) < 21) {
...
handone.push_back(...);
handtwo.push_back(...);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.