How to generate a sequence in vector with C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

double valueGenerator()                  
{
	static double val = 1.0;

	val += val;

	return val;
}

int main()
{
	vector<double> vec(6);

	generate(vec.begin(), vec.end(), valueGenerator);     

	for (double val : vec)
		cout << val << " ";
	
	cout << endl;

	return 0;
}

/*
run:

2 4 8 16 32 64

*/

 



answered May 2, 2018 by avibootz

Related questions

...