1

[-1.0, 1.0] などの間隔を、指定された距離または各ポイント間のステップ サイズを持つ等間隔のポイントの離散セットに分割したいと思います。たとえば、ステップ サイズが 0.1 の場合、関数は配列を返します。

-1.0, -0.9, -0.8,...., 0.9, 1.0. 

ベクターコンテナを使用する方法は次のとおりです。

vector<double> Range;      

double Step = 0.1;

Range.push_back(-1.0); 

for(unsigned int i=0; i<int(2.0/Step); i++)
{
    Range.push_back(Range.back() + Step); 
}

使用できる STL ライブラリ関数はありますか?

4

2 に答える 2

0

以下は、generate 関数を使用した例です。

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <math.h>

using namespace std;

// The step interval
const double step = 0.1;

struct stepInterval {

   double current;

   stepInterval() :
      current (-1.0 - step) {
   }

   double operator()() {
      current += step;
      return current;
   }

} StepInterval;



int main () {
   // Calculate the vector size. Add 1 for zero.
   const int vectorSize = ceil(2.0 / step) + 1;

   // Create the vector
   vector<double> myvector (vectorSize);

   generate (myvector.begin(), myvector.end(), StepInterval);

   cout << "\nmyvector contains:";

   for (vector<double>::iterator it=myvector.begin();
        it!=myvector.end();
        ++it)  {
      cout << " " << *it;
   }
   cout << endl;

   return 0;
}
于 2010-08-25T18:20:10.790 に答える
0

機能をチェックしてくださいgenerate。インクリメント用の関数/ファンクターを作成する必要がありますが、これは比較的単純で、いつでも再利用できます...

于 2010-08-25T08:50:20.800 に答える