【发布时间】:2014-05-29 16:13:25
【问题描述】:
应该有可能,因为short ints 的数量少于std::set<short int> 的max_size():
#include <iostream>
#include <set>
#include <limits.h>
#include <math.h>
int main()
{
std::set<short int> mySet;
std::cout << "Max size of mySet is " << mySet.max_size() << std::endl;
std::cout << "Number of short ints is " << pow(2.0, double(sizeof(short int) * CHAR_BIT));
return 0;
}
输出
Max size of mySet is 4294967295
Number of short ints is 65536
现在,我想做的是将所有short ints 放入一个集合中,这意味着我需要一个例程来迭代所有short ints。如果可能的话,我想要一个通用的例程来迭代给定类型的所有元素。有人有一些见识吗?
【问题讨论】:
-
为什么需要这个?我的意思是,它有什么目的?
-
因此循环 0 - 65535 并将每个值添加到您的集合中。
-
遍历所有短整数,将每个短整数插入您的集合。
-
std::numeric_limits为您提供该类型的最低和最高可能值;从那里开始,这是一个微不足道的循环。 -
@DonaldKnuth:如果你想让别人免费为你做这件事,它需要有一个目的。此外,冒充我们领域的名人也是不受欢迎的。
标签: c++