Initializing a Vector (C++)

Initializing a Vector (C++)

本文关键字:C++ Vector Initializing      更新时间:2023-10-16

我一直在尝试创建一个函数,该函数将数字转换为向量(确切地说7),并计算平均值。然而,我已经尝试将每个向量元素初始化为用户输入的数字。例子:

double average(int a, int b, int c, int d, int e, int f, int g)
vector<double>aa;
aa[0]=int a;
aa[1]=int b;
aa[2]=int c; etc.

但是当我这样做时,我得到

错误:在' int '

之前期望主表达式

改成

for(size_t j=0; j<=7; j++)
{
    cout<<"Enter item # " <<(j+1);
    cin>>a[j];
}

数组下标的int[size_t{又名unsigned int}]类型无效cin>> [j],

我是编程新手,可能不理解复杂的答案。如果之前有人问过这个问题,我很抱歉。我使用Linux Ubuntu来编译我的代码(Terminal, g++)。

注意:我们的讲师的答案是:

double average (const vector<int>& v)
{
    int N = v.size();
    int sum = 0;
    for (int i = 0; i < N; i = i + 1)
    {
        sum = sum + v[i];
    }
    return static_cast<double>(sum)/(N); 
}

(我认为我的是错误的,因为他要求"引用函数调用",我认为我创建了一个"值调用"。)我不知道他写了什么(尤其是return static_cast<double>(sum)/(N);)

你真的应该拿起你的课本,开始阅读,最好是昨天。不过,我将分别讨论每一部分。

你的代码(稍作修改)

double average(int a, int b, int c, int d, int e, int f, int g)
{
    vector<double>aa;
    aa[0]=int a;
    aa[1]=int b;
    aa[2]=int c;
    // etc.
}

有一些问题。你收到的错误是由于所有的行看起来像aa[0]=int a;, int这个词不应该在那里。如果您删除它们,它应该编译,并在运行时崩溃,因为aa的大小仍然为0。您可以预先分配向量(vector<double> aa(7);)或使用aa.push_back(aa);

第二次尝试

for(size_t j=0; j<=7; j++)
{
    cout<<"Enter item # " <<(j+1);
    cin>>a[j];
}

对于初学者,第一行应该是for(size_t j=0; j<7; j++)(缺少=)。a应该是第一次尝试的vector<double>,而不是函数参数。

你的讲师代码有一行

double average (const vector<int>& v)

v之前的&告诉函数,它没有收到变量v的副本(按值),而是收到原始副本(按引用)。通常,这意味着如果函数改变了它的值,那么调用函数的副本也会改变。然而,这里有一个神奇的词const,它禁止函数更改值。

最后,static_cast<double>是将变量类型从int转换(更改)为double的一种形式。原因是3/2 == 13.0/2 == 1.5 .

const vector<int>& v ->这表示通过const引用传递元素类型为int的向量,即您不能更改v(添加/删除/插入/调整大小)

for部分是for循环,从0循环到v.size()-1,即c++中的数组索引从0开始,向量索引也是如此。因此第一个元素是v[0],依此类推…

sum = sum + v[i] ->执行v的所有元素的和。

最后,你需要除以v中的元素个数来计算平均值。但是用int除以std::size_t不会得到带小数点的结果。因此需要将值强制转换为double

因此static_cast<double>(sum)/(N) .

对于初始化std::vector,使用如下代码:

std::vector<int> v ;
int temp = 0 ;
while(true) {
    std::cin >> temp ;
    if(temp == -1)
        break ;
    v.push_back(temp);
}

上面的代码将读取输入并插入到vector中,直到遇到-1。

代码中编译器错误的原因

vector<double>aa;
aa[0]=int a;
aa[1]=int b;
aa[2]=int c;

表示在每个赋值中存在int关键字。移除它们。

编译器不会抱怨的另一个问题是默认创建大小为0的vector。下一个问题是vectoroperator[](由表达式aa[0]等调用)不会根据需要神奇地调整向量的大小。如果索引无效,则行为未定义。

因此,在给vector的元素赋值之前,必须先给vector赋值。

vector<double>aa (some_integer_greater_than_2);
aa[0]=a;
aa[1]=b;
aa[2]=c;

vector<double>aa;
aa.resize(some_integer_greater_than_2)
aa[0]=a;
aa[1]=b;
aa[2]=c;

如果事先不知道vector的大小,可以使用push_back()(在vector的末尾添加一个元素,根据需要调整大小)。

vector<double>aa;
aa.push_back(a);    //since initial size is zero, this (in effect) resizes and copies a to aa[0]
aa.push_back(b);    //since size is now 1, this (in effect) resizes and copies b to aa[1]
aa.push_back(c);    //since size is now 2, this (in effect) resizes and copies c to aa[1]

我是一个排名初学者,但刚刚从那些长时间的"为什么所有的名字都是好的和油的,这不能工作"的时刻,试图将整数输入到一个定义最大大小的向量,我提供了这个笔记。我的错误在于,在定义vector numbs(25)的最大大小时,我无意中使用了括号而不是括号(即vector numbs[25]),这导致了在输入阶段出现最令人困惑的失败(即cin>> numbs[I])。下面是主题代码:

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int size, i;
    vector<int> numbs(25); //error was using brackets instead of parens
    cout << "Please enter the size of the vector you wish to create (<25)";      
    cin >> size; 
    cout << "Please enter " << size << " integers: ";
    for (i=0; i<size; i++)
        cin >> numbs[i];  // where the effect of the prior error showed up
    return 0;
}