为什么喜欢构图而不是继承呢?每种方法都有哪些权衡取舍?什么时候应该选择继承而不是作文?
我有一个阅读文件并输出平均考试成绩的作业.
这很简单,但我不喜欢平均值是如何完成的.
average = (test1 + test2 + test3 + test4 + test5) / 5.0;
Run Code Online (Sandbox Code Playgroud)
有没有办法让它除以测试分数?我在书中或谷歌找不到这样的东西.就像是
average = (test + test + test + test) / ntests;
Run Code Online (Sandbox Code Playgroud) 从这个问题的答案中,作为一个例子,这是一个计算元素总和的代码std::vector:
std::for_each(
vector.begin(),
vector.end(),
[&](int n) {
sum_of_elems += n;
}
);
Run Code Online (Sandbox Code Playgroud)
我知道 lambda函数只是无名函数.
我理解 lambda函数语法,如下所述.
我不明白为什么lambda函数需要捕获列表,而普通函数则不需要.
而不是打字
array[0] + array[1] //.....(and so on)
Run Code Online (Sandbox Code Playgroud)
有没有办法将数组中的所有数字相加?我正在使用的语言是c ++我希望能够以更少的输入来完成它,而不是我刚输入的语言.
可能重复:
a中元素的总和std::vector
我有std::vector<int>,我想计算该向量中所有值的总和.是否有内置函数或我需要编写自定义代码?
我有以下代码
#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
int main()
{
typedef std::vector<int> Vector;
int sum=0;
Vector v;
for(int i=1;i<=10;++i)
v.push_back(i);
std::tr1::function<double()> l=[&]()->double{
std::for_each(v.begin(),v.end(),[&](int n){sum += n; //Error Here in MSVC++});
return sum;
};
std::cout<<l();
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码产生错误,MSVC++ 10而它编译得很好g++ 4.5.产生的错误是1 IntelliSense: invalid reference to an outer-scope local variable in a lambda body c:\users\super user\documents\visual studio 2010\projects\lambda\lambda.cpp 19 46 lambda
那么,有没有其他方法可以访问外部作用域变量sum而无需在本地lambda表达式(内部std::for_each)中显式创建新变量?
在g++ 4.5代码编译好.标准(n3000草案)是否说了什么?(我目前没有C++ - 0x(1x?)标准的副本)
我尝试以vector<vector<int>>非循环的方式实现对a的所有元素的总结。
之前,我已经检查了一些相关的问题,如何总结C ++向量的元素?。
因此,我尝试使用std::accumulate它来实现它,但是我发现很难重载Binary Operatorin std::accumulate并实现它。
因此,我对如何实现它感到困惑,std::accumulate还是有更好的方法?
如果不介意有人可以帮助我吗?
提前致谢。
可能重复:
a中元素的总和std::vector
我想总结std :: vector的项目
例如
std::vector<int > MYvec;
/*some push backs*/
int x=sum(MYVec); //it should give sum of all the items in the vector
Run Code Online (Sandbox Code Playgroud)
怎么写sum功能?
我试过这个
int sum(const std::vector<int> &Vec)
{
int result=0;
for (int i=0;i<Vec.size();++i)
result+=Vec[i];
return result;
}
Run Code Online (Sandbox Code Playgroud)
但是我不喜欢我的做法
我如何找到用户输入的向量中元素的总和?我试图寻找一种方法可以在网上到处都可以找到,但是在网上找不到真正能很好地解释它的方法,也没有在课堂上解释得太多。
所以我基本上是在这里由用户输入的向量,但是我不知道如何使用它来求和?(仅存在printvector,因为在告诉用户总和之前,我必须向用户展示用户输入的内容)
#include <iostream>
#include <vector>
using namespace std;
void fillVector(vector<int>&);
void printVector(const vector<int>&);
int main()
{
vector<int> VectorQuantities;
fillVector(VectorQuantities);
printVector(VectorQuantities);
return 0;
}
void fillVector(vector<int>& newVectorQuantities)
{
cout << "Type in a list of numbers, and type in -1 as the last number when you are finished: ";
int input;
cin >> input;
while (input != -1) {
newVectorQuantities.push_back(input);
cin >> input;
}
cout << endl;
}
void printVector(const vector<int>& newVectorQuantities) {
cout << "Vector: ";
for (unsigned int …Run Code Online (Sandbox Code Playgroud) 我想获得一些陀螺仪读数的平均值,它涉及将一个std::vector<double>类型与一个双精度类型相除,但是出现以下错误,该错误报告
无效的二进制二进制操作数(“ std :: vector”和“ double”)
我该如何解决?
double n_readings;
std::vector<double> gyro_reading;
for(int i = 0; i < n_readings; i++) {
gyro_reading.push_back(gyro_z());
msleep(1);
}
double average = gyro_reading/n_readings;
Run Code Online (Sandbox Code Playgroud) c++ ×9
vector ×3
c++11 ×2
lambda ×2
addition ×1
aggregation ×1
algorithm ×1
arrays ×1
composition ×1
inheritance ×1
oop ×1
operators ×1
stdvector ×1
stl ×1
sum ×1
visual-c++ ×1