100 objects

Hello I have two files Principal.cpp and Individuo.h, I want to create a hundred objects of Individuo, what can I do?
Thanks

Principal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include "Individuo.h"

using namespace std;

vector<Individuo> individuo;
vector<string> fact={"r","M","sin(teta)","1","2"};
vector<string> operacion={"+","-","*","/","^"};

int main()
{	
	for(int k=0;k<100;k++)
	{	
	//cout<<"i="<<i<<endl;
		individuo(fact[i],operacion[j]);

	}
	
		
	return 0;
}



and Individuo is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<vector>
#include<string>

using namespace std;

class Individuo{
public:
	Individuo(string f, string o)
		:factor{f},operacion{o}{}
	void setFactor(string factor)
	{
		this->factor=factor;
	}
	void setOperacion(string operacion)
	{
		this->operacion=operacion;
	}
private: 
	string factor;
	string operacion;
};		
> vector<string> fact={"r","M","sin(teta)","1","2"};
There are 5 of these

> vector<string> operacion={"+","-","*","/","^"};
There are 5 of these as well.

I make 25 possible permutations.

How do you get to 100?


Let's start here, with something that would actually compile?
1
2
3
4
5
6
7
8
	vector<Individuo> individuo;
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			individuo.push_back(Individuo(fact[i], operacion[j]);
		}
	}


As salem c mentioned, if you choose 1 from each set, you can have 25 unique pairs. Anything more than that, and you're going to have repeated elements. But of course you still can make 100 elements worth.

1
2
3
4
5
6
7
8
	vector<Individuo> individuo;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			individuo.push_back(Individuo(fact[i % 5], operacion[j % 5]);
		}
	}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

class Individuo{
public:
	Individuo(string f, string o)
		:factor{f},operacion{o}{}
	void setFactor(string factor)
	{
		this->factor=factor;
	}
	void setOperacion(string operacion)
	{
		this->operacion=operacion;
	}
private: 
	string factor;
	string operacion;
};	

vector<Individuo> individuo;
vector<string> fact={"r","M","sin(teta)","1","2"};
vector<string> operacion={"+","-","*","/","^"};

int main()
{	
	for(int k=0;k<100;k++)
	{	
	    individuo.push_back(Individuo("string one","string two"));

	}
	
		cout << individuo.size();
	return 0;
}


Here is 100 objects of individuo.
Every individuo have random sctrings of fact and strings ...
Every individuo have random sctrings of fact and strings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Individio.h

#include<iostream>
#include<vector>
#include<string>

class Individuo
{
public:
   Individuo(std::string f, std::string o)
      :factor { f }, operacion { o }{}

   void setFactor(std::string factor)
   {
      this->factor = factor;
   }

   void setOperacion(std::string operacion)
   {
      this->operacion = operacion;
   }

   std::string GetFactor() const
   {
      return factor;
   }

   std::string GetOperacion() const
   {
      return operacion;
   }

private:
   std::string factor;
   std::string operacion;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>
#include <chrono> // for std::system::clock
#include <random> // for std::default_random_engine & std::uniform_int_distribution

#include "Individuo.h"

int main()
{
   // construct a trivial random generator engine from a time-based seed:
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
   std::default_random_engine rng(seed);

   std::vector<std::string> fact      = { "r","M","sin(teta)","1","2" };
   std::vector<std::string> operacion = { "+","-","*","/","^" };

   // create two distributions for selecting random elements from the string vectors
   std::uniform_int_distribution<int> fact_dist(0, fact.size() - 1);
   std::uniform_int_distribution<int> operacion_dist(0, operacion.size() - 1);

   std::vector<Individuo> individuo;

   for (size_t i = 0; i < 100; i++)
   {
      individuo.push_back(Individuo(fact[fact_dist(rng)], operacion[operacion_dist(rng)]));
   }

   for (size_t i = 0; i < individuo.size(); i++)
   {
      std::cout << individuo[i].GetFactor() << ", " << individuo[i].GetOperacion() << ".\t";

      if ((i + 1) % 5 == 0)
      {
         std::cout << '\n';
      }
   }
   std::cout << '\n';
}

r, -.   2, ^.   M, /.   r, -.   1, -.
1, ^.   sin(teta), -.   1, /.   sin(teta), *.   1, +.
2, *.   M, ^.   sin(teta), ^.   sin(teta), *.   r, *.
1, *.   2, /.   1, +.   1, /.   sin(teta), +.
1, /.   1, ^.   sin(teta), +.   1, -.   2, +.
sin(teta), -.   r, -.   1, *.   2, *.   r, /.
2, -.   1, -.   1, -.   2, -.   M, +.
1, *.   1, +.   r, -.   sin(teta), /.   r, -.
2, -.   M, +.   sin(teta), /.   sin(teta), /.   r, +.
1, -.   2, ^.   sin(teta), /.   1, +.   2, ^.
1, -.   2, /.   sin(teta), /.   1, *.   1, -.
sin(teta), /.   sin(teta), /.   2, *.   M, ^.   M, *.
M, +.   sin(teta), /.   2, *.   r, *.   sin(teta), /.
2, ^.   M, -.   2, *.   1, /.   M, /.
M, -.   M, ^.   2, +.   r, -.   M, ^.
sin(teta), ^.   sin(teta), ^.   sin(teta), *.   r, /.   M, +.
sin(teta), /.   sin(teta), +.   M, /.   r, *.   M, *.
sin(teta), -.   1, -.   r, -.   1, +.   r, *.
1, ^.   M, /.   2, ^.   sin(teta), ^.   2, /.
2, -.   r, -.   r, *.   2, /.   M, +.
Last edited on
Every individuo have random sctrings of fact and strings ...

Is that an observation or requirement?
If the latter, why did you not say so in the first post?
Topic archived. No new replies allowed.