std vector size keep ground 虽然我插入相同的索引
std vector size keep ground Although i insert in the same indexs
我在此处看到带有标准矢量的东西
我有
其值动态变化但始终小于 20 的变量
示例中的 dynamicSizeToInsert。
为什么矢量大小不断增长?
std::vector<int> v;
//sometimes its 5 sometimes it is 10 sometimes it is N < 20
int dynamicSizeToInsert = 5
int c = 0;
for(std::vector<int>::size_type i = 0; i != 100; i++) {
if(c == dynamicSizeToInsert )
{
c = 0;
}
v.insert(v.begin() + c, c);
c++;
printf("%d",v.size()) //THIS THINK KEEP growing although i only using vector indexes 0 to 4 allways
}
我想保持我的向量边有 5 个元素大
并且该新值将 运行 超过同一索引中的其他值。
std::vector::insert 将一个新成员插入数组中您指定的索引处,并向前移动其他元素,甚至在达到容量时重新分配数组(相对昂贵的操作)
The vector is extended by inserting new elements before the element at
the specified position, effectively increasing the container size by
the number of elements inserted.
This causes an automatic reallocation of the allocated storage space
if -and only if- the new vector size surpasses the current vector
capacity.
(http://www.cplusplus.com/reference/vector/vector/insert/)
如上所述,每次插入操作都会扩展向量。
要获得您想要的行为,您需要像这样使用 [] 运算符:
v[i] = some_new_value;
这样就不会添加新元素,它只会更改第 i 个元素的值。
insert 不会替换元素,而是在给定位置插入元素并将所有正确的元素向右移动一个位置。这就是您的矢量大小不断增长的原因。
如果要替换现有索引,则可以使用 operator[]。但是,请记住索引必须在 0 - size() - 1
之间才能使用 operator[]
.
std::vector::insert
,顾名思义,在指定位置插入个元素。
当c == dynamicSizeToInsert
时,c
设置为0
。所以现在,v.size() == 5
。现在这行执行:
v.insert(v.begin() + c, c);
这将在位置 v.begin() + 0
处插入 0
,即位置 0
并且它将偏移所有其他元素(它将 而不是 替换位置 0
) 的元素,因此向量不断增长。
不使用 insert
,而是使用 operator[]
:
//So that 'v' is the right size
v.resize(dynamicSizeToInsert);
for(std::vector<int>::size_type i = 0; i != 100; i++) {
if(c == dynamicSizeToInsert )
{
c = 0;
}
v[i] = c; //Sets current index to 'c'
c++;
}
const int dynamicSizeToInsert = 5;
std::vector<int> v(dynamicSizeToInsert);
int c = 0;
for(std::vector<int>::size_type i = 0; i !=100; i++)
{
v.at(i%dynamicSizeToInsert) = (dynamicSizeToInsert == c?c = 0,c ++: c ++);
printf("%d",v.size());
}
我在此处看到带有标准矢量的东西 我有 其值动态变化但始终小于 20 的变量 示例中的 dynamicSizeToInsert。 为什么矢量大小不断增长?
std::vector<int> v;
//sometimes its 5 sometimes it is 10 sometimes it is N < 20
int dynamicSizeToInsert = 5
int c = 0;
for(std::vector<int>::size_type i = 0; i != 100; i++) {
if(c == dynamicSizeToInsert )
{
c = 0;
}
v.insert(v.begin() + c, c);
c++;
printf("%d",v.size()) //THIS THINK KEEP growing although i only using vector indexes 0 to 4 allways
}
我想保持我的向量边有 5 个元素大 并且该新值将 运行 超过同一索引中的其他值。
std::vector::insert 将一个新成员插入数组中您指定的索引处,并向前移动其他元素,甚至在达到容量时重新分配数组(相对昂贵的操作)
The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.
This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity. (http://www.cplusplus.com/reference/vector/vector/insert/)
如上所述,每次插入操作都会扩展向量。 要获得您想要的行为,您需要像这样使用 [] 运算符:
v[i] = some_new_value;
这样就不会添加新元素,它只会更改第 i 个元素的值。
insert 不会替换元素,而是在给定位置插入元素并将所有正确的元素向右移动一个位置。这就是您的矢量大小不断增长的原因。
如果要替换现有索引,则可以使用 operator[]。但是,请记住索引必须在 0 - size() - 1
之间才能使用 operator[]
.
std::vector::insert
,顾名思义,在指定位置插入个元素。
当c == dynamicSizeToInsert
时,c
设置为0
。所以现在,v.size() == 5
。现在这行执行:
v.insert(v.begin() + c, c);
这将在位置 v.begin() + 0
处插入 0
,即位置 0
并且它将偏移所有其他元素(它将 而不是 替换位置 0
) 的元素,因此向量不断增长。
不使用 insert
,而是使用 operator[]
:
//So that 'v' is the right size
v.resize(dynamicSizeToInsert);
for(std::vector<int>::size_type i = 0; i != 100; i++) {
if(c == dynamicSizeToInsert )
{
c = 0;
}
v[i] = c; //Sets current index to 'c'
c++;
}
const int dynamicSizeToInsert = 5;
std::vector<int> v(dynamicSizeToInsert);
int c = 0;
for(std::vector<int>::size_type i = 0; i !=100; i++)
{
v.at(i%dynamicSizeToInsert) = (dynamicSizeToInsert == c?c = 0,c ++: c ++);
printf("%d",v.size());
}