c++向量数组/ while循环



我正在阅读c++编程在简单步骤4和已经读到的东西,我有困难的理解。我前几天才开始读这本书。

当我完成这本书的那一部分时,我有一段特别困难的时间来理解向量数组和元素,尽管花了很多时间来尝试理解它们,但我仍然对它没有信心,所以我计划再次访问它。我现在在书的后面部分演示循环,这是我遇到麻烦的代码(只有一行):

#include <iostream>
#include <vector>       // Include vector support (Vector function library)
using namespace std;
int main()
{

    //Declaring an integer vector and an integer variable for loop counter
    vector <int> vec ( 10 );
    int i = 0;

    //Now inserting a while loop to assign a counter value to an element of the vector on each iteration
    while ( i < vec.size() )
    {
        i++;                 //Increment the counter
        vec[ i -1 ] = i;     // Assign count to element
        cout << " | " << vec.at ( i -1 );
    }

    return 0;
}

我什么都明白,除了这一行:

        vec[ i -1 ] = i;     // Assign count to element

我不确定这到底是做什么特别是I -1部分?有人能给我解释一下吗?我将快速地重新访问向量数组部分,看看我是否能够理解。

矢量索引从0开始。一个包含10个元素的向量从0索引到9。假设您的目标是用数字110连续加载数组,并且由于您从0开始i并在索引表达式中使用它之前增加它,因此您需要从中减去1以获得正确的索引。

这段代码做同样的事情。

vector<int> vec(10);
int i = 0;
while (i < vec.size())
{
    vec[i] = i + 1;
    i++;
}
i = 0;
while (i < vec.size())
{
    cout << i << ": " << vec.at(i) << endl;
    i++;
}

如果您将代码分解为while循环实际上通过单个语句完成的内容(如果您使用调试器运行while循环并添加诸如i-1之类的watch语句,您将看到类似的情况),它可能会对您有所帮助:

#include <iostream>
#include <vector>       // Include vector support (Vector function library)
using namespace std;
int main()
{

    //Declaring an integer vector and an integer variable for loop counter
    vector <int> vec ( 10 ); // vector of 10 elements: 0...9
    int i = 0; // i starts with the value of 0
    // replace while loop with the individual statements the loop accomplishes
    // note also that vec.size() = 10
    // while (i < vec.size())
    // increment i to 1 (started as 0, from above)
    i++;                 //Increment the counter
    vec[ 0 ] = i;     // i - 1 = 0 here - Assign count to element
    cout << " | " << vec.at ( 0 );
    // increment i to 2
    i++;                 //Increment the counter
    vec[ 1 ] = i;     // i - 1 = 1 here - Assign count to element
    cout << " | " << vec.at ( 1 );
    // continue to i = 9 ...
    // increment i to 10
    i++;                 //Increment the counter
    vec[ 9 ] = i;     // i - 1 = 9 here - Assign count to element
    cout << " | " << vec.at ( 9 );    
    return 0;
}

你将数字1到10赋值给你的向量。

vec[i-1] = i

直接跟在i++后面,这意味着,对于你的第一个索引,你分配的值比索引多一个计数/步。简单地说,让我们遵循i的前4个值和插入到vector中的元素:

i = 0;
i = 1;here vec[ 1 - 1] = vec[0] = 1
i = 2;here vec [2 - 1] = vec[1] = 2
i = 3;and finally vec[ 2 ] = 3

vector是数组的包装类(来源:c++参考链接),它还做了一些非常酷的事情,比如为分配器使用模板,提供动态空间,并使用stl迭代器。

因此,可以像数组一样使用[]操作符访问向量。这两个代码片段是相同的:

首先,数组形式:

//Allocate and assign array
int* arr[5]={1,2,3,4,5};
//Print the 3rd element, note that the first index corresponds to 0.
std::cout<< arr[2] <<std::endl;
//Reassign the 2nd element:
arr[1]=54;

现在是vector版本:

//Allocate and assign vector
std::vector<int> arr( {1,2,3,4,5} );
//Print the 3rd element, note that the first index corresponds to 0.
std::cout<< arr[2] <<std::endl;
//Reassign the 2nd element:
arr[1]=54;

给聪明的人一个提示:像这样访问向量可能是危险的,因为数组的大小可以在代码的其他地方改变。相反,可以考虑以下赋值:

std::vector<int> arr( {1,2,3,4,5} );
int index=0;
for(std::vector<int>::iterator it = arr.begin(); it!= arr.end(); ++it){
    if(index++ == 3){
        (*it) = 54;
    }
}

最新更新