因此用户在 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++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
fpermissive 标志有什么作用?What does the fpermissive flag do?(fpermissive 标志有什么作用?)
如何在我不想编辑的第 3 方代码中禁用来自 gccHow do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?(如何在我不想编辑的第 3 方代码中禁
使用 GCC 预编译头文件Precompiled headers with GCC(使用 GCC 预编译头文件)
如何在 OS X 中包含 omp.h?How to include omp.h in OS X?(如何在 OS X 中包含 omp.h?)
如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文How can I make GCC compile the .text section as writable in an ELF binary?(如何让 GCC 将 .text 部分编译为可写在 ELF 二进制文件中?)
GCC、字符串化和内联 GLSL?GCC, stringification, and inline GLSL?(GCC、字符串化和内联 GLSL?)