C++矢量-push_back

发布于 2024-10-14 23:47:18 字数 364 浏览 8 评论 0原文

在《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

轮廓§ 2024-10-21 23:47:18

for循环真的赋值0吗
元素的值?或者,我们必须
使用push_back函数?

ivec[ix] =0 更新向量中现有元素的值,而 push_back 函数向向量添加新元素!

那么,以下内容有效吗?
ivec[ix] = ix;

这是完全有效的 IF ix < ivec.size()。

如果您使用迭代器而不是索引,那就更好了。像这样,

int ix = 0;
for(vector<int>::iterator it = ivec.begin() ; it != ivec.end(); ++it)
{ 
     *it = ix++; //or do whatever you want to do with "it" here!
}

在 STL 中使用iterator 是惯用的。优先选择迭代器而不是索引!

Is the for-loop really assigning 0
values to the elements? Or, we have to
use the push_back finction?

ivec[ix] =0 updates the value of existing element in the vector, while push_back function adds new element to the vector!

So, is the following valid?
ivec[ix] = ix;

It is perfectly valid IF ix < ivec.size().

It would be even better if you use iterator, instead of index. Like this,

int ix = 0;
for(vector<int>::iterator it = ivec.begin() ; it != ivec.end(); ++it)
{ 
     *it = ix++; //or do whatever you want to do with "it" here!
}

Use of iterator with STL is idiomatic. Prefer iterator over index!

四叶草在未来唯美盛开 2024-10-21 23:47:18

是的,您可以使用方括号来检索和覆盖向量的现有元素。但请注意,您不能使用方括号将新元素插入到向量中,事实上,索引超过向量末尾会导致未定义的行为,通常会导致程序彻底崩溃。

要增长向量,您可以使用 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.

朦胧时间 2024-10-21 23:47:18

使用数组括号,矢量对象的行为就像任何其他简单数组一样。 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.

酒绊 2024-10-21 23:47:18

此 for 循环的目的是迭代向量的元素。
从元素 x(当 ix 为 0 时)开始到最后一个元素(当 ix 为 ivec.size() -1 时)。

在每次迭代中,向量的当前元素设置为 9。
这就是声明的作用

ivec[ix] = 0;

。放入

ivec[ix] = ix;

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

ivec[ix] = 0;

does. Putting

ivec[ix] = ix;

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.

寄风 2024-10-21 23:47:18

是的,假设 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文