【发布时间】:2014-02-21 07:05:12
【问题描述】:
void pre_process(string& pattern, vector<int>& c)
{
c.reserve(pattern.length());
c[0] = -1;
c[1] = 0;
for(int i=2; i<pattern.length(); ++i)
{
if(pattern[c[i-1]] == pattern[i-1])
c[i] = c[i-1]+1;
else
c[i] = 0;
}
cout << c.size() << endl; //why the size is zero here?
}
在向量中保留空间后,我将值分配给向量的不同位置。所以不应该增加尺寸吗?
将vector用作定长容器的正确方法是什么?
【问题讨论】:
-
“我正在为向量的不同位置赋值”——不,你不是;您正在调用 UB。
reserve没有resize()。 -
简短摘要:您看到向量的大小为零,因为向量的大小为零。
标签: c++ vector stl containers