Jump to content

Understanding vectors

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
Ok, so this time it's the C++ container class vector I am referring to.

Would this be legal and correct?


Random rnd(10);

vector<Particle> dot(n, Particle(rnd));


I.e. will that create a vector of n-objects of class Particle, constructed with the constructor Particle(rnd)?

Also, would that be the appropriate way to pass an object of type Random to the Particle constructor? In the constructor, a member function of object rnd is called.
Will one copy of Random rnd be made for every object of Particle? That is not what I need, since the Random object is only used for generating a better pseudo-random number than rand(); Should I use pass-by-pointer or pass-by-reference instead? Wouldn't i have to manually create a copy of the pointed-to object in order to be able to call the member function? I feel I SHOULD know the answer to this, but right now I'm very confused.

The pseudo-random code i use is found here: Better Random Number Class - C++ - Source Code | DreamInCode.net

Also, is this a valid and correct way of accessing the elements in the vector?

dot.at(n).show(screen);

I will post more code if I have to, but I feel that should be sufficient for my questions. Besides, it's not finished nor neat as of now ;)
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
It's legal, but I don't think that it'll do what you think it will. Remember that C++ passes arguments by value, so instead of passing the Constructor function Particle(rnd) in itself, it'll generate one Particle with rnd as it's argument and copy that for each Particle in the vector. Here's an idea to get what you want:

template <typename T, typename U> 
int fill_vector_with(std::vector<T>& vec, T (*func)(int, U), U arg, size_t num)
{
    std::vector<T> copy(vec);
    try
    {
        vec.clear();
        for (int iii = 0; iii < num; ++iii)
            vec.push_back( (*func)(iii, arg) );
    }
    catch (...)
    {
        /* This is to make it an atomic function. */
        vec = copy;
        return -1;
    }
    return 0;
}

Particle gen_particle(int iter, Random* rnd)
{
    return Particle(rnd->gen());
}

int main()
{
    Random rnd(10);
    std::vector<Particle> dot;
    if (fill_vector_with(dot, gen_particle, &rnd, 10))
        cout << "Failed to generate vector.\n";
    /* do something now... */
}

That should solve the problem of filling any kind of vector with what you'd need and using whichever parameters you need to fill it. I hope that made sense, and there's probably something in the standard library that does something similar, it was just faster for me to write this simple thing. :P
Wow I changed my sig!

#3
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
Ah, that's what confused me! Your code makes perfect sense, thank you!
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde

#4
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
I would probably just make random reference member of particle.
Like so:


class Random

{

public:

	// not relevant

	// ...


}; // class Random



class Particle

{

public:

	explicit Particle( Random& random )

		: random_( random )

	{

	}


	Particle( const Particle& other )

		: random_( other.random_ )

	{

	}


	Particle& operator=( const Particle& other )

	{

		if ( this != &other )

		{

			random_ = other.random_;

		} // if


		return *this;

	}


	~Particle( void )

	{

	}



private:

	Random& random_;


}; // class Particle



int main( int argc, char* argv[] )

{

	Random random;


	std::vector< Particle > particles( 10, Particle( random ));

	///

	///	-> creates temporary particle

	///		-> vector copy constructs n particles from temp one

	///		-> pushes them in the vector

	///	-> every particle has a reference to the Random instance created above



	return 0;

}



Hope this helps. (:
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
More than one way to skin a cat, julmuri. :)
Wow I changed my sig!

#6
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

ZekeDragon said:

More than one way to skin a cat, julmuri. :)
Yeah I suppose, your way is better if you only need the object for constructing the particle. (:
std::string s("oberq zhpu?");std::for_each(s.begin(),s.end(),[&](char&c){c=~c;c=~c-0x01/(~(c|0x20)/0x0D*0x02-0x0B)*0x0D;});std::cout<<s;

#7
Walle

Walle

    Learning Programmer

  • Members
  • PipPipPip
  • 75 posts
Hmm, that seems like another nice way to solve it! Thank you!
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde