std :: vector:需要保留? [英] std::vector: reserve required?
问题描述
在std :: vector中,需要保留还是调整大小?
开启:
Linux mbrc32 2.6.22.1-41.fc7#1 SMP Fri
7月27日18:10:34 EDT 2007 i686 athlon
i386 GNU / Linux
使用:
g ++ (GCC)4.1.2 20070502(Red Hat 4.1.2-12)
以下程序失败,但如果保留(en)
未被注释, 有用。这是预期的吗?
// vectst.cc 07/04/08
#include< iostream>
#include< vector>
using namespace std;
int main(int argc,const char * argv [])
{
int en = 10;
vector< int vec;
// vec.reserve(zh-CN );
for(int jj = 0; jj< en; ++ jj)
vec [jj] = jj;
for (int jj = 0; jj< en; ++ jj)
cout<< vec [jj]<<结束;
退出(0);
}
谢谢,
Mike。
In std::vector, is reserve or resize required?
On:
Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
Jul 27 18:10:34 EDT 2007 i686 athlon
i386 GNU/Linux
Using:
g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
The program below fails, but if the reserve(en)
is uncommented, it works. Is this as expected?
// vectst.cc 07/04/08
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;
// vec.reserve(en);
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;
exit (0);
}
Thanks,
Mike.
推荐答案
Mike - Email Ignored写道:
Mike -- Email Ignored wrote:
在std :: vector中,需要保留还是调整大小?
开:
Linux mbrc32 2.6.22.1-41.fc7#1 SMP星期五
7月27日18:10:34 EDT 2007 i686 athlon
i386 GNU / Linux
使用:
g ++(GCC)4.1.2 20070502(Red Hat 4.1.2-12)
以下程序失败,但如果保留(en)
是没有注释,它的工作原理。这是预期的吗?
// vectst.cc 07/04/08
#include< iostream>
#include< vector>
using namespace std;
int main(int argc,const char * argv [])
{
int en = 10;
vector< int vec;
// vec.reserve(zh-CN );
for(int jj = 0; jj< en; ++ jj)
vec [jj] = jj;
for (int jj = 0; jj< en; ++ jj)
cout<< vec [jj]<<结束;
退出(0);
}
谢谢,
麦克风。
In std::vector, is reserve or resize required?
On:
Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
Jul 27 18:10:34 EDT 2007 i686 athlon
i386 GNU/Linux
Using:
g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
The program below fails, but if the reserve(en)
is uncommented, it works. Is this as expected?
// vectst.cc 07/04/08
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;
// vec.reserve(en);
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;
exit (0);
}
Thanks,
Mike.
不,你可以这样做(使用保留),或者,使用push_back()
int main(int argc ,const char * argv [])
{
int en = 10;
vector< int vec;
>
for(int jj = 0; jj< en; ++ jj)
vec.push_back(jj);
for(int jj = 0 ; jj< en; ++ jj)
cout<< vec [jj]<< endl;
返回0;
}
No, you could do that (use reserve), or, use push_back()
int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;
for (int jj = 0; jj < en; ++jj)
vec.push_back(jj);
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;
return 0;
}
Mike - Email Ignored写道:
Mike -- Email Ignored wrote:
在std :: vector中,需要保留还是调整大小?
In std::vector, is reserve or resize required?
不,但你需要以某种方式填充向量。你可以在
构建时,或者使用push_back,insert或resize来实现。
另一方面,reserve()方法没有增长向量。
No, but you need to fill the vector somehow. You can do that either upon
construction, or by using push_back, insert, or resize.
The reserve() method, on the other hand does not grow the vector.
>
开:
Linux mbrc32 2.6.22.1-41.fc7# 1 SMP Fri
Jul 27 18:10:34 EDT 2007 i686 athlon
i386 GNU / Linux
使用:
g ++(GCC)4.1.2 20070502(Red Hat 4.1.2-12)
以下程序失败,但如果保留(en)
没有注释,它有效。
>
On:
Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri
Jul 27 18:10:34 EDT 2007 i686 athlon
i386 GNU/Linux
Using:
g++ (GCC) 4.1.2 20070502 (Red Hat 4.1.2-12)
The program below fails, but if the reserve(en)
is uncommented, it works.
它没有。您正在观察未定义行为的表现。
It does not. You are observing a manifestation of undefined behavior.
这是否符合预期?
Is this as expected?
对于未定义的行为将如何表现,没有任何期望。
从实施的质量来看,如果你在打开断言的情况下编译程序,我会希望看到一个中止
。
There are no expectations as to how undefined behavior will show itself.
From a quality of implementation point of view, would want to see an abort
if you compile the program with assertions turned on.
// vectst.cc 07 / 04/08
#include< iostream>
#include< vector>
using namespace std;
int main(int argc,const char * argv [])
{
int en = 10;
vector< int vec;
// vec.reserve(en);
for(int jj = 0; jj< en; + + jj)
vec [jj] = jj;
for(int jj = 0; jj< en; ++ jj)
cout<< vec [jj]<< ENDL;
// vectst.cc 07/04/08
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char* argv[])
{
int en = 10;
vector<int vec;
// vec.reserve(en);
for (int jj = 0; jj < en; ++jj)
vec[jj] = jj;
for (int jj = 0; jj < en; ++jj)
cout << vec[jj] << endl;
尝试类似
cout<< vec.size()<<结束;
并思考你得到的含义。
Try something like
cout << vec.size() << endl;
and ponder the meaning of what you get.
>
退出(0);
}
>
exit (0);
}
Best
Kai-Uwe Bux
Best
Kai-Uwe Bux
http ://www.cplusplus.com/reference/stl/vector/operator [] .html
http://www.cplusplus.com/reference/stl/vector/operator[].html
这篇关于std :: vector:需要保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!