C++矢量-push_back
在《C++ Primer》一书的第 (3) 章中,有以下 for 循环将向量中的元素重置为零。
vector<int> ivec; //UPDATE: vector declaration
for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix)
ivec[ix] = 0;
for 循环是否真的为元素分配 0
值,还是必须使用 push_back
函数?
那么,以下内容有效吗?
ivec[ix] = ix;
谢谢。
In the C++ Primer book, Chapter (3), there is the following for-loop that resets the elements in the vector to zero.
vector<int> ivec; //UPDATE: vector declaration
for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix)
ivec[ix] = 0;
Is the for-loop really assigning 0
values to the elements, or do we have to use the push_back
function?
So, is the following valid?
ivec[ix] = ix;
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ivec[ix] =0
更新向量中现有元素的值,而push_back
函数向向量添加新元素!这是完全有效的 IF
ix < ivec.size()。
如果您使用迭代器而不是索引,那就更好了。像这样,
在 STL 中使用
iterator
是惯用的。优先选择迭代器而不是索引!ivec[ix] =0
updates the value of existing element in the vector, whilepush_back
function adds new element to the vector!It is perfectly valid IF
ix < ivec.size()
.It would be even better if you use
iterator
, instead of index. Like this,Use of
iterator
with STL is idiomatic. Prefer iterator over index!是的,您可以使用方括号来检索和覆盖向量的现有元素。但请注意,您不能使用方括号将新元素插入到向量中,事实上,索引超过向量末尾会导致未定义的行为,通常会导致程序彻底崩溃。
要增长向量,您可以使用 push_back、insert、resize 或 allocate 函数。
Yes, you can use the square brackets to retrieve and overwrite existing elements of a vector. Note, however, that you cannot use the square brackets to insert a new element into a vector, and in fact indexing past the end of a vector leads to undefined behavior, often crashing the program outright.
To grow the vector, you can use the push_back, insert, resize, or assign functions.
使用数组括号,矢量对象的行为就像任何其他简单数组一样。
push_back()
将其长度增加一个元素,并将新的/最后一个元素设置为您传递的值。Using the array brackets the vector object acts just like any other simple array.
push_back()
increases its length by one element and sets the new/last one to your passed value.此 for 循环的目的是迭代向量的元素。
从元素 x(当 ix 为 0 时)开始到最后一个元素(当 ix 为 ivec.size() -1 时)。
在每次迭代中,向量的当前元素设置为 9。
这就是声明的作用
。放入
for 循环会将向量的所有元素设置为其在向量中的位置。即,第一个元素的值为 0(因为向量从 0 开始索引),第二个元素的值为 1,依此类推。
The purpose of this for loop is to iterate through the elements of the vector.
Starting at element x (when ix is 0) up to the last element (when ix is ivec.size() -1).
On each iteration the current element of the vector is set to 9.
This is what the statement
does. Putting
in the for loop would set all the elements of the vector to their position in the vector. i.e, the first element would have a value of zero (as vectors start indexing from 0), the second element would have a value of 1, and so on and so forth.
是的,假设 ix 是一个有效的索引,很可能:您有一个 int 向量,并且索引是 size_type。当然,有时您可能想故意存储 -1 以显示无效索引,因此无符号到有符号的转换是合适的,但我建议使用 static_cast。
执行您正在执行的操作(将向量中的每个值设置为其索引)是创建其他集合的索引的一种方法。然后,您可以根据其他集合的预测重新排列向量排序。
假设您从不溢出(如果您的系统是 32 位或更多,则不太可能)您的转换应该可以进行。
Yes, assuming ix is a valid index, most likely: you have a vector of int though and the index is size_type. Of course you may want to purposely store -1 sometimes to show an invalid index so the conversion of unsigned to signed would be appropriate but then I would suggest using a static_cast.
Doing what you are doing (setting each value in the vector to its index) is a way to create indexes of other collections. You then rearrange your vector sorting based on a predicte of the other collection.
Assuming that you never overflow (highly unlikely if your system is 32 bits or more) your conversion should work.