What is the size of sizeof(vector)? C++(sizeof(vector) 的大小是多少?C++)
问题描述
因此用户在 for 循环中输入值,向量将其推回,创建自己的索引.问题出现在第二个 for 循环中,我认为它必须对 sizeof(v)/sizeof(vector) 做一些事情.
So the user inputs values within the for loop and the vector pushes it back, creating its own index. The problem arises in the second for loop, I think it has to do something with sizeof(v)/sizeof(vector).
vector<int> v;
for (int i; cin >> i;)
{
v.push_back(i);
cout << v.size() << endl;
}
for (int i =0; i < sizeof(v)/sizeof(vector); i++)
{
cout << v[i] << endl;
}
输入值后如何确定向量的大小?(我对 C++ 很陌生,所以如果我犯了一个愚蠢的错误,我道歉)
How will I determine the size of the vector after entering values? (I'm quite new to C++ so If I have made a stupid mistake, I apologize)
推荐答案
使用 v.size()vector::size() 方法:i .
sizeof 运算符返回编译时对象或表达式的大小(以字节为单位),对于 std::vector 来说是常量.
The sizeof operator returns the size in bytes of the object or expression at compile time, which is constant for a std::vector.
这篇关于sizeof(vector) 的大小是多少?C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:sizeof(vector) 的大小是多少?C++
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
