- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
在 C++ Primer
书,第 (3) 章,有以下 for 循环将 vector 中的元素重置为零。
for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix)
ivec[ix] = 0;
为什么使用 vector<int>::size_type ix = 0
?我们不能说int ix = 0
?在第二个表单上使用第一个表单有什么好处?
谢谢。
最佳答案
C++ 标准说,
size_type | unsigned integral type | a type that can represent the size of the largest object in theallocation model
然后添加,
Implementations of containers described in this International Standard are permitted to assume that their Allocator template parameter meets the following two additional requirements beyond those in Table 32.
- The typedef members pointer, const_pointer, size_type, and difference_type are required to be T*,T const*, size_t, and ptrdiff_t, respectively
所以很可能,size_type
是 size_t
的 typedef。
标准确实将其定义为,
template <class T>
class allocator
{
public:
typedef size_t size_type;
//.......
};
所以最需要注意的点是:
size_type
是 unsigned
整数,而 int
是 非 必然 未签名
。 :-)关于C++ for 循环 - size_type 与 size_t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4849678/
在我的 for 循环中声明迭代器时研究无符号整数与有符号整数比较警告时,I read this : Whenever possible, use the exact type you will be
我有一个 std::map, float>这占用了太多内存,为了使用更少的内存,我决定将唯一字符串映射到整数(例如 std::map ,其中每个新的唯一字符串都映射到 map 的当前 size() )
文档说,std::vector 的 size_type 是/usually/size_t,这是合理的,因为实现可以选择使用不同的。 但是为什么 size_type = size_t 位于 std::a
以下代码片段编译失败: #include #include #include #include using namespace std; vector list1{1,3,5,7,11}; s
我正在编写自定义算法,有时它需要获取两个迭代器之间的距离。如果我假设 it1
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
std::vector的成员类型为size_type。它的printf格式说明符是什么? 请注意,size_type与size_t不同。 https://en.cppreference.com/w/c
我在这里浏览了一些关于 string::size_type 的线程,根据 C++ 标准,我确实理解这个 size_type 保证了所有字符串使用的足够分配。 我只是觉得很难相信。如果我将 C++ Pr
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个类,它有一个类型为 vector 的私有(private)数据成员. 该类有两个实际使用vector::size_type 的公共(public)方法: 返回 vector 中元素个数的方法
在 C++ 模板中,我很难用正确的 size_type 定义变量。基本上,这将是容器中的索引类型。我知道 int 可以工作,但希望以干净的形式使用它。 template void test(Forwa
假设我有两种类型,Class1和 Class2 .然后我创建两个 vector : vector vec1; vector vec2; //create vec1 and vec2, such tha
我在一些非常有名的 C++ 书籍中看到 -- vector ivec; for (vector::size_type i = 0; i != 10; ++i) { ivec.push_back
我想以一种简单的方式编写基于索引的 for 循环。由于我的代码必须在 32 位和 64 位中运行,我收到很多关于隐式转换的警告,因为我通常使用 32 位整数,但 STL 在 64 位中使用 64 位整
我的背景主要是 R、SAS 和 VBA,我正在尝试学习一些 C++。我选择了“Accelerated C++”(Koenig,Moo)作为我关于该主题的第一本书。我在 comp 的理论背景。科学。诚然
下面的类不编译: template, class Allocator = std::allocator> class MyContainer { public: std::vector d
我经常有一些类,它们大多只是一些 STL 容器的包装器,如下所示: class Foo { public: typedef std::vector Vec; typedef Vec::size
显然,unordered_set::erase和 unordered_set::count返回一些不是严格 bool 值的东西(从逻辑上讲,也就是说,我不是在谈论实际类型)。 链接页面读取第三个版本的
#include #include using namespace std; int main() { vector student_marks(20); for (vector:
我一直在通过在不同平台上编译我的应用程序来对其进行一些测试,从 64 位系统到 32 位系统的转变暴露出许多问题。 我大量使用 vector 、字符串等,因此需要对它们进行计数。但是,我的函数也使用
我是一名优秀的程序员,十分优秀!