Vector of pointers to class objects dynamic allocation and dellocation



  • class CSNP{
    public:
        static bool checkCorrectMapfile(string line);
    // ...
    };
    


    • Gugelmoser schrieb:
    class CSNP{
    public:
        static bool checkCorrectMapfile(string line);
    // ...
    };
    

    Hi Gugelmoser, thank you so much! would you please explain what kind of miracle this static in the code

    static bool checkCorrectMapfile(string line);
    

    did and it worked.why it didn't work without static?

    best wishes
    euklid



  • euklid schrieb:

    • Gugelmoser schrieb:
    class CSNP{
    public:
        static bool checkCorrectMapfile(string line);
    // ...
    };
    

    Hi Gugelmoser, thank you so much! would you please explain what kind of miracle this static in the code

    static bool checkCorrectMapfile(string line);
    

    did and it worked.why it didn't work without static?

    • I tried with static but checkCorrectMapfile is still not doing what I want this function to do. Even though I have four columns in the map file, it is still saying that:
      "your map file was expected to have 4 columns but this is not the case"
      I do not know why.

    best wishes
    euklid

    class Person
    {
        private:
            double weight;
    
        public:
            Person(double w) : weight(w) {}
            double whats_my_weight() { return weight; };    
    };
    
    int main()
    {
        /*
            In general, you'll always have to greate an object.
            Look, what sense would it make if you do Person::whats_me_weight()... if you have no person, you can't have any weight.
        */
        Person p(88.5);
        cout << "I weight " << p.whats_my_weight();
    
        /*
            But sometimes you want a function to be independent from a object... then static comes into play, like in your case.
            You want to call a function with its class name using the scope operator: you want to do CSNP::checkCorrectMapfile(line)
            --> thus you want a function being independent from an object.
        */
    }
    

    I hope that helps a bit.



  • class Person
    {
        private:
            double weight;
        
        public:
            Person(double w) : weight(w) {}
            double whats_my_weight() { return weight; };    
    };
    
    int main()
    {
        /*
            In general, you'll always have to greate an object.
            Look, what sense would it make if you do Person::whats_me_weight()... if you have no person, you can't have any weight.
        */
        Person p(88.5);
        cout << "I weight " << p.whats_my_weight();
        
        
        /*
            But sometimes you want a function to be independent from a object... then static comes into play, like in your case.
            You want to call a function with its class name using the scope operator: you want to do CSNP::checkCorrectMapfile(line)
            --> thus you want a function being independent from an object.
        */
    }
    

    I hope that helps a bit.

    Thank you so much! I appreciate your explanation!

    • I tried with static but checkCorrectMapfile is still not doing what I want this function to do. Even though I have four columns in the map file, it is still saying that:
      "your map file was expected to have 4 columns but this is not the case".
    • I found that vector<string> tokens has 0 size. This means there is no stream any more in when I call this function. But I still do not know what to do. I think after line 40 of the code
    stringstream line_parser(line);
    

    I should not write

    if(CSNP::checkCorrectMapfile(line))
    

    Probably the line has no value any more. However, I still do not know how to solve this problem.



  • edit: Murks



  • thank you so much for kind response. it works fine but in this way, I have to open and read the file two times: once when I want for checkCorrectMapfile and once for the function

    ostream& operator<<(ostream &out, const CSNP &csnp)
    

    since my file is very large, I am interested to read the file only once to check if the correctness of test.map and then save it as an object of class type.
    using the following function.

    stream& operator>>(istream& in, CSNP &csnp){
        string line;
        while (getline(in, line) and (!line.size() or line[0] == '#'));
        stringstream line_parser(line);
        if(CSNP::checkCorrectMapfile(line)) // this line is not working
        line_parser >> csnp.nchr >> csnp.snpName >> csnp.allele1 >> csnp.allele2;
        else
            line_parser >> csnp.snpName >> csnp.allele1 >> csnp.allele2;
        if (!line_parser) in.setstate(ios::failbit);
        return in;}
    

    However, it is not working. I have written this problem as a simple example. If anyone could find the solution of the following problem, I would be thankful.
    The problem is is written as comments in lines 22, 23 and 27 of the following program

    #include<iostream>
    #include<sstream>
    #include<string>
    #include<vector>
    #include<cstdlib>
    using namespace std;
    int main(){
    	string line;
    	string name, famName, buff;
    	vector<string> tokens;
    	cout << " enter your name: ";
    	while(cin){
    		getline(cin, line);
    		if(line=="0")exit(1);
    	cout << "your name is: "<< line <<".\n"; 
    	stringstream ss(line);
    		while (ss>> buff)
    		tokens.push_back(buff);
    		for(int i=0; i<tokens.size();i++)
    			cout <<" "<<tokens[i];
    			cout <<endl;
    		/* I want to use ss again but it is not working 
    		 Is the life time of ss expired now?
    		 */
    		ss>>name >> famName;
    	cout <<"name: " <<name << " and "<<"famName: "<<famName <<".\n";
            // name and famName have  no values assigned. why?       
    
    	}	
    return 0;
    }
    

    ps: There is no compiling error. Only that name and famName have no values assigned at the end.



  • Just to clarify:

    1. There is a mapfile given just like that:

    1 snp1 0 1
    3 snp2 0 2
    2 snp3 1 1
    2 snp4 2 2
    3 snp5 1 2
    

    2. You want to check wheather the mapfile is built correctly.

    3. You want to read the mapfile and store its content into CSNP-Objects.

    4. You want to output all the CSNP-Objects.

    Is this what you're trying to do or are we tearing past each other?



  • Gugelmoser schrieb:

    Just to clarify:
    1. There is a mapfile given just like that:

    1 snp1 0 1
    3 snp2 0 2
    2 snp3 1 1
    2 snp4 2 2
    3 snp5 1 2
    

    2. You want to check wheather the mapfile is built correctly.

    3. You want to read the mapfile and store its content into CSNP-Objects.

    4. You want to output all the CSNP-Objects.

    Is this what you're trying to do or are we tearing past each other?

    Yes Gugelmoser, you are absolutely right.
    [list]
    [1] I have a bool parameter with default value false say

    bool threeColumns =false
    

    if threeColumns=true, the map file looks like

    snp1 0 1
     snp2 0 2
     snp3 1 1
     snp4 2 2
     snp5 1 2
    

    In this case I want that the default value of #nchr is for example 0.

    [2]If threeColumns=false, then

    1 snp1 0 1
    3 snp2 0 2
    2 snp3 1 1
    2 snp4 2 2
    3 snp5 1 2
    

    [3] I want first check for the correct mapfile like I did before and then I want to save them as class a vector of class objects.
    [4] The map file is very large at least 10000,000 rows.

    int main()
    {
      vector<CSNP> genInfo;
      {
        CSNP value;
        ifstream file("test.map");
        while (file >> value)
          genInfo.push_back(value);
      }
    

    where each object CSPP looks like

    class CSNP
    {
    private:
      int nchr;
      string snpName;
      string allele1;
      string allele2;
      friend istream& operator>>(istream& in, CSNP &csnp);
    // this I was trying to define like before. 
    
     };
    


  • Well i was bored and that's the result (you're looking for) 😃

    #include <iostream>
    #include <string>
    #include <fstream>
    #include <sstream>
    #include <vector>
    using namespace std;
    
    /*
        mapfile:
            1 snp1 0 1
            3 snp2 0 2
            2 snp3 1 1
            2 snp4 2 2
            3 snp5 1 2
    */
    
    class CSNP
    {
        private:
            static bool three_columns; 
            int nchr;
            string snpName;
            string allele1;
            string allele2;
    
        public:
            static bool checkCorrectMapfile(const string&);
            CSNP() : nchr(0) {}
            friend istream& operator>>(istream& in, CSNP& csnp) 
            {
                if(!three_columns)
                {
                    in>>csnp.nchr;
                }
                getline(in,csnp.snpName,' ');
                getline(in,csnp.allele1,' ');
                return getline(in,csnp.allele2);
            }
            friend ostream& operator<<(ostream &out, const CSNP &csnp)
            {
                return out << csnp.nchr << ' ' << csnp.snpName << ' ' << csnp.allele1 << ' ' << csnp.allele2;
            }      
    };
    
    bool CSNP::three_columns = false;
    bool CSNP::checkCorrectMapfile(const string& mapfile)
    {
    
        ifstream in( mapfile.c_str() );
    
        if(!in)
        {
            cerr << "could not open mapfile";
            return false;
        }
        else
        {
            vector<string> lines;
            string buffer;
    
            while( getline(in,buffer) )
            {
                lines.push_back(buffer);
            }
    
            for(vector<string>::iterator it=lines.begin(); it!=lines.end(); ++it)
            {
                istringstream iss(*it);
                vector<string> tokens;
    
                while( getline(iss,buffer,' ') )
                {
                    tokens.push_back(buffer);
                }
    
                if(tokens.size() != 3 && tokens.size() != 4)
                {
                    cerr << "your mapfile was expected to have either 3 or 4 columns!";
                    return false;
                }
    
                if( !three_columns && tokens.size() == 3)
                {
                    three_columns = true;
                }
            }
        }
        cout << "your mapfile is correct!";
        return true;
    }
    
    int main()
    {
        string mapfile = "...";
    
        if( CSNP::checkCorrectMapfile(mapfile) )
        {
            ifstream in( mapfile.c_str() );
            vector<CSNP> objects;
    
            for(CSNP csnp; in >> csnp;)
            {
                objects.push_back(csnp);
                cout << '\n' << csnp;
            }
    
            // now, every line is stored into a CSNP-Object.
        }
    
        return 0;
    }
    

    To be honest, i don't know wheater the design is or is not good.
    Anyhow, reading the file only once... i didn't manage to get any good design for that :(.



  • thank you so much for your help. I have posted new code which is error free but not efficient.


Anmelden zum Antworten