当我使用向量名称后跟包含整数变量的括号时,括号是什么意思? [已关闭]

问题描述 投票:0回答:1

Stackoverflow 太狗屎了

我目前正在阅读《编程原理》一书。它向我展示了一些矢量示例,例如以下代码:

#include    "std_lib_facilities.h"
#include    <iostream>
#include    <string>
#include    <vector>

using namespace std;

int main()
{
    vector <double> temps;

    double temp = 0;
    double sum = 0;
    double high_temp = 0;
    double low_temp = 0;

    while (cin >> temp)
        temps.push_back(temp);

    for (int i = 0; i<temps.size(); ++i)
    {
        if (temps[i] > high_temp) high_temp = temps[i];
        if (temps[i] < low_temp) low_temp = temps[i];
        sum += temps[i];
    }

    cout << "High temperature: " << high_temp << endl;
    cout << "Low temperature: " << low_temp << endl;
    cout << "Average temperature: " << sum / temps.size() << endl;

    return 0;
}

我不知道当它使用

temps[i]
int i = 0
放在括号内时意味着什么。那么,它说明了什么?我希望我已经说清楚了。

c++ vector brackets
1个回答
3
投票

temps[i]
表示您正在尝试访问向量中的第“i”个元素。所以如果
i=0
那么
temps[i]
就意味着
temps[0]

© www.soinside.com 2019 - 2024. All rights reserved.