#ifndef PARTICLE_H
#define	PARTICLE_H
#include<vector>
#include<iostream>
using namespace std;
class Particle
{
protected:
	
public:
	Particle();
	
	Particle(const int aDim);

	int dimension;

	vector<double> velocity;
	
	vector<double> position;

	vector<double> pBest;
	
	double pBestFitness;
	
	double fitness;
	
	bool valid;

	bool operator<(const Particle& aP2) const;

	~Particle(void);
};

#endif
#define	SWARM_H

#include"Particle.h"
#include<vector>
#include <Algorithm>
#include<iostream>
#include<stdexcept>
using namespace std;

class Swarm:public vector<Particle>
{
protected:
	unsigned int swarmSize;
public:
	Swarm();

	Swarm(const int& asize);

	~Swarm(void);
	
	unsigned int& getSwarmSize();

	void append(const unsigned int newsize);

      struct Cmp
          {     
			  bool operator()(const Particle & a,const Particle & b) const
                { return a<b; }
          };

	 const Particle & bestParticle() const;

};

#endif

Dear friends:
I have two classed, the class particle class has dimension, postion, and velocity. The other class is swarm, it is a swarm of particles.
I can declare a new swarm class as:
Swarm aSwarem(asize);
My problem is how to set the particle's dimension. How to modify the constructor of the swarem.
Regards/.

Recommended Answers

All 3 Replies

What I understand from your requirement is that your Swarm object will contain Particle(s). Each Particle has a unique dimension_id. Why can't you use map in Swarm class. A map where the key is dimension_id and value is a Particle object.

Please correct me if my understanding of your requirement is wrong.

What I understand from your requirement is that your Swarm object will contain Particle(s). Each Particle has a unique dimension_id. Why can't you use map in Swarm class. A map where the key is dimension_id and value is a Particle object.

Please correct me if my understanding of your requirement is wrong.

thank you very much for your reply.
vector is more conveinent than map.

I'd say inheriting after STL classes is a bad idea, but that may be just my prejudice.
Consider, what would happen if I were to call Swarm::resize() - your swarmSize member would get invalid instantly.

Well, every particle in the swarm will have the same dimension, won't it? It would be easy if you had vector<Particle> as a member, not a parent then. Just store the dimension inside your swarm class, and pass it as a parameter to the constructor.

But right now the implications of inheriting after std::vector are a bit too hard for me to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.