Initializing a Vector (C++)
Initializing a Vector (C++)
我一直在尝试创建一个函数,该函数将数字转换为向量(确切地说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 == 1
而3.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。下一个问题是vector
的operator[]
(由表达式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;
}
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中用vector填充一个简单的动态数组
- vector.resize()中的分配错误
- 使用std::vector的OpenCL矩阵乘法
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 在某些循环内使用vector.push_back时出现分段错误
- 当vector是tje全局变量时,c++中vector的内存管理
- std::vector的包装器,使数组的结构看起来像结构的数组
- 为什么(-1)%vector::size()总是返回0
- 在C++中将类(带有Vector成员)保存为二进制文件
- 编译器如何区分std::vector的构造函数
- 将 int 数组转换为 std::vector<int*>
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 在std::vector上存储带有模板的类实例
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 为什么std::vector比数组慢
- std::vector::迭代器是否可以合法地作为指针
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么std::vector和std::valarray初始化构造函数不同