Question about containers use

This is a discussion on Question about containers use within the C++ Programming forums, part of the General Programming Boards category; Hi, is it correct to use vector , for example, with no dynamically allocated objects? Code: #include <iostream> #include <vector> ...

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    Question about containers use

    Hi,

    is it correct to use vector, for example, with no dynamically allocated objects?
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Mean{
    public:
    	Mean(int a, int b){ mean = (a+b)/2.0; }
    private:
    	float mean;
    };
    
    int main()
    {
    	vector<Mean> means;
    	for(int i=0 ; i<5 ; i++){
    		means.push_back(Mean(i, i+1));
    	}
    	return 0;
    }
    I see a problem, correct me if I am wrong, you create two objects in the statment
    Code:
    means.push_back(Mean(i, i+1));
    . The first is created to be passed to the function, and the second, the copy constructor is called.

    Must I use pointers instead? Or this is good programming?
    Thanks any insight.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The push_back() is prefectly ok. An un-named object of type Mean is created an it's added to the vector. You can use pointers in a vector but from my experience its a lot more trouble then it's worth.

    I see nothing wrong with the way you are putting values into the vector.

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Thanks!

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Just make sure your class has a decent copy constructor (if neccessary) since std::vector may need to copy the data if it grows larger.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>since std::vector may need to copy the data if it grows larger.
    It would seem to me that you need a copy constructor for adding elements to the vector, and an assignment operator for subsequent resizes. The copy constructor is generated automatically unless you explicitly define one, and so is the assignment operator. In this case you have no problem, since the only data contained in the class is a POD (plain 'ol data) type, float. However, if you have dynamically allocated memory etc. and a destructor that frees it, then you'll have problems unless you define your own copy constructor/assignment operator - or unless you decide to go the route of storing pointers in the container.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Ok! Thanks!

    P.S.: Magos, your link in your signature seems to be broken.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    hunter2: Yes, a default plain-copy copy constructor is automatically created. What I meant by "decent" were a hand-made one if some dynamic allocation took place.

    mortissus: Has been for half a year. Been too lazy to update sigs on all the forums I visit :P
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21