C++ 初始化向量(C+;+;)

C++ 初始化向量(C+;+;),c++,vector,C++,Vector,我一直在尝试创建一个函数,将数字输入到向量中(准确地说是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. 双倍平均(整数a、整数b、整数c、整数d、整数e、整数f、整数g) 向量a; aa[0]=int a

我一直在尝试创建一个函数,将数字输入到向量中(准确地说是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.
双倍平均(整数a、整数b、整数c、整数d、整数e、整数f、整数g)
向量a;
aa[0]=int a;
aa[1]=intb;
aa[2]=int c;等
但当我这么做的时候,我会

错误:“int”之前应为主表达式

当我把它改成

for(size_t j=0; j<=7; j++)
{
    cout<<"Enter item # " <<(j+1);
    cin>>a[j];
}
for(size_t j=0;j
常量向量&v
->这表示通过
常量
引用传递元素类型为
int
的向量,即不能更改
v
(添加/删除/插入/调整大小)

< <代码> > <代码>部分是for循环,它从<代码> 0代码>代码>代码> >(1)/代码>,即C++中的数组索引从0开始,向量也为索引。因此,第一个元素是v[0 ],所以……/p>
sum=sum+v[i]
->这执行v的所有元素的求和

最后,您需要除以v中的元素数来计算平均值。但是将
int
除以
std::size\u t
不会得到小数点后的结果。因此,您需要将值转换为
双精度

因此
static\u cast(sum)/(N)

对于初始化
std::vector
,请使用以下方法:

std::vector<int> v ;
int temp = 0 ;
while(true) {
    std::cin >> temp ;
    if(temp == -1)
        break ;
    v.push_back(temp);
}
std::vector v;
内部温度=0;
while(true){
标准:cin>>温度;
如果(温度==-1)
打破
v、 推回(温度);
}
上述代码将读取输入并插入向量,直到遇到-1。

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

< <代码> > <代码>部分是for循环,它从<代码> 0代码>代码>代码> >(1)/代码>,即C++中的数组索引从0开始,向量也为索引。因此,第一个元素是v[0 ],所以……/p>
sum=sum+v[i]
->这执行v的所有元素的求和

最后,您需要除以v中的元素数来计算平均值。但是将
int
除以
std::size\u t
不会得到小数点后的结果。因此,您需要将值转换为
双精度

因此
static\u cast(sum)/(N)

对于初始化
std::vector
,请使用以下方法:

std::vector<int> v ;
int temp = 0 ;
while(true) {
    std::cin >> temp ;
    if(temp == -1)
        break ;
    v.push_back(temp);
}
std::vector v;
内部温度=0;
while(true){
标准:cin>>温度;
如果(温度==-1)
打破
v、 推回(温度);
}

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

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

您的代码(略有更改)


对于初学者来说,第一行应该是For(size_t j=0;j你真的应该拿起你的课本,开始阅读,最好是昨天。不过,我将分别介绍每个部分

您的代码(略有更改)


对于初学者,第一行应该是
(size_t j=0;j代码中编译器错误的原因)

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


代码中编译器错误的原因

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


我是一个等级初学者,但我刚刚经历了一个漫长的“为什么在所有好的和油腻的东西的名义下,这不起作用”的时刻,试图将整数输入到一个定义了最大大小的向量中,我给出了这个注释。我的错误是在定义向量的最大大小时——向量numbs(25)--我无意中使用了括号而不是括号--即向量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;
}
#包括
#包括
使用名称空间std;
int main()
{
int大小,i;
向量numbs(25);//错误是使用括号而不是括号
大小;

cout我是一个等级初学者,但我刚刚经历了一个漫长的“为什么在所有好的和油腻的东西的名义下,这不起作用”的时刻,试图将整数输入到一个定义了最大大小的向量中,我给出了这个注释。我的错误是在定义向量的最大大小时——向量numbs(25)--我无意中使用了括号而不是括号--即向量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;
}
#包括
#包括
使用名称空间std;
int main()
{
int大小,i;
向量numbs(25);//错误是使用括号而不是括号
大小;

你有一个问题陈述,你的讲师提供了正确的答案。你的问题是什么?他的回答让我困惑。你有一个问题陈述,你的讲师提供了正确的答案。你的问题是什么?他的回答让我困惑。
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<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]
#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;
}