Vector of pointers to class objects dynamic allocation and dellocation



  • 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