【发布时间】:2013-09-22 04:20:50
【问题描述】:
我正在尝试编写一个用于枚举特定基数的函数,其中数字存储在某种列表中。这是一个例子,取一个 std::vector
void next_value(std::vector<unsigned int> &num, unsigned int base) {
unsigned int carry = 1;
for (unsigned int &n: num) {
n += carry;
if (n >= base) {
carry = 1;
n = 0;
} else {
carry = 0;
}
}
}
num 向量不一定必须是向量,它可以是数组,或者实际上任何具有为其定义了 std::begin() 和 std::end() 的类型。有没有办法用 begin() 和 end() 来表示 num 可以是任何东西,但它的元素必须具有 unsigned int 类型?
【问题讨论】:
-
只要把代码写成值类型对就好了。如果有人试图在持有错误类型的容器上调用该函数,他们将得到错误。