Thread: Simple list class question

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    12

    Simple list class question

    I have a list which is for storing all the data, same as normal class, have something like .get() and .set() for getting and setting value to the list in the program. However, at the moment, I can only predefined the size of the list within the code. How may I put the definition in the function itself so the size of the list may depends on the input?

    And may I ask another question, why in modulation the definition of variable is not in the main but in the individual module?

    Thank you very much!!


    Code:
    #include <time.h>
    #include <stdlib.h>
    #include <cmath>
    #include <iostream>
    #include <istream>
    #include <fstream>
    
    using namespace std;
    
    	//class of list of vacancy
    	vacancylist fvacancy[50000];
    
    // *******************Begin output final vacancy list*******************
    
    int outputv()//(i j k)
    {	
    	nv=0;
    	na=0;
    	for(i=1; i<=ns; i++)
    	{
    		if(site[i].getstate()==0)
    		{

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What class? I see nothing but an array (not "linked" list) of some undefined type.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    This is the class vacancy which is used, thanks!!

    Code:
    #include <iostream> // Include input/output stream
    #include <fstream>  // Include file output stream
    #include <cmath>   // Include math function, sin, cos
    class vacancylist
    {
    private:
    	int nnconfig, clusterno, clustersize, xcoord, ycoord, zcoord, note, note2;
    	double note3, tempx, tempy, tempz; // Private data members
    public:
    	vacancylist() 
    	{
    		nnconfig= 0;
    		clustersize=0;
    		clusterno=0;
    		xcoord = 0;
    		ycoord = 0;
    		zcoord = 0;
    		note=0;
    		note2=0;
    		note3=0;
    		tempx=0;
    		tempy=0;
    		tempz=0;
    	}
    	int getnnconfig()
    	{
    		return nnconfig;
    	}
    	int getclustersize()
    	{
    		return clustersize;
    	} 
    	int getclusterno()
    	{
    		return clusterno;
    	}
    	int getx()
    	{
    		return xcoord;
    	}
    	int gety()
    	{
    		return ycoord;
    	}
    	int getz()
    	{
    		return zcoord;
    	}
    	int getnote()
    	{
    		return note;
    	}
    	int getnote2()
    	{
    		return note2;
    	}
    	double getnote3()
    	{
    		return note3;
    	}
    	double gettempx()
    	{
    		return tempx;
    	}
    	double gettempy()
    	{
    		return tempy;
    	}
    	double gettempz()
    	{
    		return tempz;
    	}
    	vacancylist(int nn, int cs, int cn, int x, int y, int z, int nb, int nb2, double nb3, double tx, double ty, double tz)
    	{
    		nnconfig=nn;
    		clustersize=cs;			//cs=cluster size
    		clusterno=cn;				//cn=cluster no
    		xcoord = x;
    		ycoord = y;
    		zcoord = z;
    		note=nb;
    		note2=nb2;
    		note3=nb3;
    		tempx=tx;
    		tempy=ty;
    		tempz=tz;
    	}
    	void setnnconfig(int value)
    	{
    		nnconfig=value;
    	}
    	void setclustersize(int value)
    	{
    		clustersize=value;
    	}
    	void setclusterno(int value)
    	{
    		clusterno=value;
    	}
    	void setx(int value)
    	{
    		xcoord=value;\
    	}
    	void sety(int value)
    	{
    		ycoord=value;
    	}
    	void setz(int value)
    	{
    		zcoord=value;
    	}
    	void setnote(int value)
    	{
    		note=value;
    	}
    	void setnote2(int value)
    	{
    		note2=value;
    	}
    	void setnote3(double value)
    	{
    		note3=value;
    	}
    	void settempx(double value)
    	{
    		tempx=value;
    	}
    	void settempy(double value)
    	{
    		tempy=value;
    	}
    	void settempz(double value)
    	{
    		tempz=value;
    	}
    	void print()
    	{
    		std::cout << nnconfig << '\t' 
    				    	<<clustersize<<'\t'
    				    	<<clusterno<<'\t'
    			      << xcoord << '\t'
    				  << ycoord << '\t'
    				  << zcoord << '\t'
    				  << note << '\t'
    				  << note2 << '\t'
    				  << note3 << '\t'					
    				  << tempx << '\t'
    				  << tempy << '\t'
    				  << tempz << std::endl;
    	}
    	void print(std::ofstream& fout)
    	{
    	fout   << nnconfig << '\t'
    		<<clustersize<<'\t'
    		<<clusterno<<'\t'
    	           << xcoord << '\t'
    			   << ycoord << '\t'
    			   << zcoord << '\t'
    			   << note << '\t'
    			   << note2 <<'\t'
    			   << note3 << '\t'
    			  << tempx << '\t'
    			  << tempy << '\t'
    			  << tempz << std::endl;
    
    
    	}
    };
    Last edited by fidodidohk; 04-29-2007 at 06:21 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    #1
    Code:
    #include <time.h>
    #include <stdlib.h>
    You should prefer these ones:
    Code:
    #include <ctime>
    #include <cstdlib>
    #2 Methods which do not modify the class, such as your "get" member functions should be declared as const, i.e.:
    Code:
    int getnnconfig() const
    {
        return nnconfig;
    }
    int getclustersize() const
    {
        return clustersize;
    }
    #3 On to the meat of your question... you should be able to easily use one of the readily available STL container classes to hold all of your vacancylist objects. These containers, such as list/vector/deque, can grow automatically to accommodate as many objects as you would need.

    #4
    Code:
    vacancylist(int nn, int cs, int cn, int x, int y, int z, int nb, int nb2, double nb3, double tx,
                double ty, double tz)
    {
        nnconfig=nn;
        clustersize=cs;			//cs=cluster size
        clusterno=cn;				//cn=cluster no
        xcoord = x;
        ycoord = y;
        zcoord = z;
        note=nb;
        note2=nb2;
        note3=nb3;
        tempx=tx;
        tempy=ty;
        tempz=tz;
    }
    vacancylist() 
    {
        nnconfig= 0;
        clustersize=0;
        clusterno=0;
        xcoord = 0;
        ycoord = 0;
        zcoord = 0;
        note=0;
        note2=0;
        note3=0;
        tempx=0;
        tempy=0;
        tempz=0;
    }
    The above two constructors can be combined into a single constructor that uses default arguments... also, where possible, initialization lists should be used to initialize the class's data members:
    Code:
    vacancylist(int nn = 0, int cs = 0, int cn = 0, int x = 0, int y = 0, int z = 0, int nb = 0,
                int nb2 = 0, double nb3 = 0.0, double tx = 0.0, double ty = 0.0,double tz = 0.0) : nnconfig(nn),
                clustersize(cs), clusterno(cn), xcoord(x), ycoord(y), zcoord(z), note(nb), note2(nb2), 
                note3(nb3), tempx(tx), tempy(ty), tempz(tz)
    {
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    Thank you very much for you reply. But I afraid I still do not know how to do the step three. Would you mind describe it a bit more explicit?
    Thank you.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    An STL container will allow you to store however many items you need to. They can grow or shrink and handle the dynamic allocation of memory all on their own.
    Code:
    #include <vector>
    
    ...
    
    std::vector<vacancylist> vlist;  // vlist is now a vector which stores vacancylist objects
    
    ...
    
    
    // Different ways to insert a vacancylist object into the vector...
    
    // Push a default/temp object onto vector
    vlist.push_back(vacancylist());  // vlist now contains 1 item
    
    // Push another temp vacancylist object onto vector
    vlist.push_back(vacancylist(1,2,3,4,5,6,7,8,1.0,2.0,3.0));  // vlist now contains 2 items
    
    // Create object first, then push onto vector
    vacancylist item(1,2,3,4,5,6,7,8,1.0,2.0,3.0);
    vlist.push_back(item);  // vlist now contains 3 items
    
    // Print the 1st object pushed onto the vector
    vlist[0].print();  // vlist[0] is 1st item in the vector, vlist[1] is 2nd, vlist[2] is 3rd
    
    // Loop through vector and print objects to a file
    std::ofstream file("foo.txt");
    for( std::vector<vacancylist>::const_iterator cit = vlist.begin(); cit != vlist.end(); ++cit )
        cit->print(file);  // print each object to the file
    A better way to do the printing is to overload operator<< for the class and then you can get rid of the two print functions...
    Code:
    class vacancylist
    {
        ...
    public:
        ...
        friend std::ostream& operator<<(std::ostream&, const vacancylist&);
    };
    
    std::ostream& operator<<(std::ostream& os, const vacancylist& item)
    {
        os << item.nnconfig << '\t' << item.clustersize << '\t' << item.clusterno << '\t'
           << item.xcoord << '\t' << item.ycoord << '\t' << item.zcoord << '\t' << item.note << '\t'
           << item.note2 << '\t' << item.note3 << '\t' << item.tempx << '\t' << item.tempy << '\t'
           << item.tempz;
        return os;
    }
    Now the printing of objects is more intuitive using the above overloaded operator, it's just like using cout to print an int but instead we will be printing a vacancylist object since we've now told the computer how to output a vacancylist object using operator<<...
    Code:
    // Print the 1st object pushed onto the vector
    std::cout << vlist[0] << std::endl;
    
    // Loop through vector and print objects to a file
    std::ofstream file("foo.txt");
    for( std::vector<vacancylist>::const_iterator cit = vlist.begin(); cit != vlist.end(); ++cit )
        file << *cit << std::endl;  // print each object to the file
    Last edited by hk_mp5kpdw; 05-01-2007 at 06:02 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    Thank you very much!! It has solved the problem!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Simple Question about Classes
    By Loctan in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2006, 02:40 AM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. Unsorted List Class - Delete Question
    By dld333 in forum C++ Programming
    Replies: 4
    Last Post: 10-26-2005, 10:55 PM
  5. Simple linked list question
    By netboy in forum C Programming
    Replies: 3
    Last Post: 07-26-2002, 09:08 PM