相关疑难解决方法(0)

喜欢构成而不是继承?

为什么喜欢构图而不是继承呢?每种方法都有哪些权衡取舍?什么时候应该选择继承而不是作文?

language-agnostic oop inheritance composition aggregation

1538
推荐指数
23
解决办法
29万
查看次数

如何在C++中获得平均值?

我有一个阅读文件并输出平均考试成绩的作业.

这很简单,但我不喜欢平均值是如何完成的.

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)

c++

28
推荐指数
1
解决办法
5万
查看次数

在lambda函数语法中,"捕获列表"的用途是什么?

这个问题的答案中,作为一个例子,这是一个计算元素总和的代码std::vector:

std::for_each(
    vector.begin(),
    vector.end(),
    [&](int n) {
        sum_of_elems += n;
    }
);
Run Code Online (Sandbox Code Playgroud)

我知道 lambda函数只是无名函数.

我理解 lambda函数语法,如下所述.

我不明白为什么lambda函数需要捕获列表,而普通函数则不需要.

  1. 捕获列表提供了哪些额外信息?
  2. 为什么普通功能不需要这些信息?
  3. lambda的功能不仅仅是无名功能吗?

c++ lambda c++11

21
推荐指数
3
解决办法
1万
查看次数

如何在C++中添加数组中的所有数字?

而不是打字

array[0] + array[1] //.....(and so on)
Run Code Online (Sandbox Code Playgroud)

有没有办法将数组中的所有数字相加?我正在使用的语言是c ++我希望能够以更少的输入来完成它,而不是我刚输入的语言.

c++ arrays addition

15
推荐指数
3
解决办法
5万
查看次数

如何计算std :: vector <int>中的值的总和

可能重复:
a中元素的总和std::vector

我有std::vector<int>,我想计算该向量中所有值的总和.是否有内置函数或我需要编写自定义代码?

c++

11
推荐指数
2
解决办法
3万
查看次数

lambda表达式(MSVC++ vs g ++)

我有以下代码

#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?)标准的副本)

c++ lambda visual-c++ visual-c++-2010 c++11

6
推荐指数
1
解决办法
3221
查看次数

如何在没有循环的C ++中总结vector int的向量

我尝试以vector<vector<int>>非循环的方式实现对a的所有元素的总结。
之前,我已经检查了一些相关的问题,如何总结C ++向量的元素?
因此,我尝试使用std::accumulate它来实现它,但是我发现很难重载Binary Operatorin std::accumulate并实现它。
因此,我对如何实现它感到困惑,std::accumulate还是有更好的方法?
如果不介意有人可以帮助我吗?
提前致谢。

c++ stdvector

4
推荐指数
2
解决办法
171
查看次数

C++设计策略中的向量和

可能重复:
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)

但是我不喜欢我的做法

c++ stl vector

3
推荐指数
1
解决办法
5992
查看次数

向量中元素的总和?

我如何找到用户输入的向量中元素的总和?我试图寻找一种方法可以在网上到处都可以找到,但是在网上找不到真正能很好地解释它的方法,也没有在课堂上解释得太多。

所以我基本上是在这里由用户输入的向量,但是我不知道如何使用它来求和?(仅存在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)

c++ sum vector

0
推荐指数
1
解决办法
2851
查看次数

如何解决此错误:二进制表达式('std :: vector &lt;double&gt;'和'double')无效的操作数?

我想获得一些陀螺仪读数的平均值,它涉及将一个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++ algorithm vector operators

-2
推荐指数
1
解决办法
149
查看次数