【问题标题】:Iteration: sum of variable and vector front, export in another vector, erase vector front迭代:变量和向量前端之和,导出到另一个向量,擦除向量前端
【发布时间】:2013-07-10 12:54:51
【问题描述】:

我正在处理我的第一个代码,但我又被一个新问题阻止了。

我在 vectorA 中有一堆值,我想执行以下 while 循环(在伪代码中):

"制作一个变量double SUM = 0和一个变量int count

取向量的front值,加到SUM"

然后擦除向量中的front

变量SUM应该充当电容器:当SUM优于给定常数u时,

SUM 等于 SUM - u

另一个向量 vectorB 会在每次 SUM > u

时存储 count 的值

现在我只有一个 VectorA,其中包含我的所有值,并将列表导出到 .txt 文件中。

我想找到一种方法,将vectorA的前值放在一个局部变量中以将其添加到SUM中,然后擦除这个前值。 那可能吗?有没有更好的方法来做到这一点?

代码如下:

#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;

// constant values

float Da=0.1; //densities
float Db=0.5;
float Dc=1;
double Dd=0.333333;

int l = 10;  //width & height
int h = 10;

float u = 10;  // UNIT

int main ()
{
    // vectors
    vector <double> VectorA;
    vector <double> vectorB;
    int I = 0;              //itération pour la somme des valeurs du vecteur
    double SUM = 0;         //somme des éléments du vecteurA

    float a = 0;
    float b = 0; // Local variables

    while (a<l+1, b<h+1){
        //values for given a & b

        double DL = Da-Da*(b/h)+Dc*(b/h);
        double DR = Db-Db*(b/h)+Dd*(b/h);
        double D  = DL-DL*(a/l)+DR*(a/l);

        //print
        //cout<<D<<endl;

        //store
        VectorA.push_back (D);

        // next pixel/unit & next line
        a = a+u;

        if (a>l) {
            a = 0;
            b = b+u;
        }
    }

    // export values to .txt file
    ofstream output_file("./step1.txt");
    ostream_iterator<double> output_iterator(output_file, "\n");
    copy(VectorA.begin(), VectorA.end(), output_iterator);
}

【问题讨论】:

    标签: c++ vector sum iteration erase


    【解决方案1】:

    让我们把所有特定领域的东西都去掉,让这更简单一些,只讨论基本问题:

    [如何]将vectorA的前值放入局部变量中进​​行添加 求和,然后抹掉这个前面的值?

    这是一个简单的方法:

    vector <double> vectorA;
    double SUM = 0.0;
    // ...
    
    while (!vectorA.empty())
    {
      const double curVal = vectorA.front();  // this strictly isn't necesarry.  the compiler might optimize this away
      SUM += curVal;
      vectorA.erase (vectorA.begin());
    }
    

    现在让我们合并u

    vector <double> vectorA;
    double SUM = 0.0;
    const double u = /* ??? */;
    
    // ...
    
    while (!vectorA.empty())
    {
      const int curVal = vectorA.front();  // this strictly isn't necesarry.  the compiler might optimize this away
      if (curVal > SUM)
      {
        SUM = curVal - u;
      }
    
      vectorA.erase (vectorA.begin());
    }
    

    我不太确定count 的行为方式,或者将哪些值存储到vectorB,但作为一个疯狂的猜测,我将假设count 每次curVal &gt; SUM 都会递增,并将 count 的新值插入到 vectorB 中。所以让我们尝试实现它:

    vector <double> vectorA;
    double SUM = 0.0;
    const double u = /* ??? */;
    int count = 0;
    vector <int> vectorB;
    
    // ...
    
    while (!vectorA.empty())
    {
      const int curVal = vectorA.front();  // this strictly isn't necesarry.  the compiler might optimize this away
      if (curVal > SUM)
      {
        SUM = curVal - u;
      ++count;
      vectorB.push_back (count);
      }
    
      vectorA.erase (vectorA.begin());
    }
    

    上面有微优化的机会,但回想一下 Knuth 的黄金法则:

    微优化是万恶之源。

    在构建软件时,最好的方法是首先选择正确的算法,同时考虑到所需的效率(无论是空间效率还是时间效率),然后以稳健、易于维护的方式进行构建。然后在发布版本中分析您的程序,识别问题热点,并仅在必要时对这些代码部分进行微优化。如果你选择正确的算法开始,并且写得好,你会经常发现不需要微优化。

    【讨论】:

    • 抱歉,myInts、myVals 和 curVal 是什么?
    • 对不起。 myInts 应该是你的vectorA,然后我把它改成了myVals,但是漏掉了一些。我会把它们都改成vectorA
    • @adrienlucca.wordpress.com:我希望现在更有意义了。
    • count 的表现如何...count 是一个int,它在 SUM > u 时在网格上“四舍五入”位置,所以count 的功能是标记这些每条宽度的矩l
    • 谢谢,但我不知道现在发生了什么,我只得到了1 作为vectorB 的输出
    猜你喜欢
    • 2021-01-02
    • 2020-12-24
    • 2013-02-09
    • 2016-05-11
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多