<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[vector of Class objects vs vector of pointers to class objects.]]></title><description><![CDATA[<p>Thanks to the two users &quot;SeppJ&quot; and &quot;Gugelmoser&quot;. With their help, I have written a complete program which can read a data file and check different errors. However, this program can't read large data file. When I read large data with like 100,00,000 lines, it tries nearly 4-5 min then it says something like bad alloc(). I do not know why? Could anyone please hint me why this program is inefficient? Any alternative program to read such data would be very appreciable.</p>
<p>The file looks as follows is as follows.</p>
<ul>
<li>having rows with usually a certain fixed number of columns,</li>
<li>different comments marked with the start letter #,</li>
<li>also contains some blank lines.</li>
<li>Blank lines and comment line should be ignored while reading.</li>
<li>The file may also contain by mistake wrong number of columns which should be corrected first as well.</li>
<li>Each row gives a class object and it should be saved as an element<br />
vector of class object.</li>
<li>the columns in each row may be either separated by both white space and by tabs mixed.</li>
</ul>
<p><strong>file content:</strong></p>
<pre><code>1 	snp1 		1.1 		1
2 snp2 1.2 2
3 snp3 1.3 3 
4 snp4 1.4 4 
# 1

#2
#hier
#3
#test
#

#
5 snp5 1.5 5 
#test
5 snp5 1.5 5 
2 test
6 snp6 1.5 6 
3ss  snp7
#test
#1
4 snp4 1.4 4 
5 snp5 1.5 5 
#
#

7 snp7 1.5 7
</code></pre>
<p>To read and display . I have written the following code.</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include&lt;fstream&gt;
#include&lt;string&gt;
#include&lt;sstream&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;
#include&lt;cstdlib&gt;

using namespace std;
 struct CSNP{
	friend istream&amp; operator&gt;&gt;(istream &amp; is, CSNP&amp; pCSNP);
	friend ostream&amp; operator&lt;&lt;(ostream &amp;out, const CSNP &amp;pCSNP);
	string nchr;
	string snpName;
	float cm_pos;
	long int bp;
	CSNP():nchr(&quot;&quot;),snpName(&quot;&quot;), cm_pos(0.0),bp(0){}
	vector&lt;CSNP&gt;lociInfo;
	static void readMapFile(const string&amp; fileName, vector&lt;CSNP&gt;locInfo);
	static void error(const string &amp; );
	static bool checkSNPinfo(const string&amp; line, const long int&amp; countSNPs);
	static bool three_columns;
		//~CSNP(); 
 };
 bool three_columns=false;
void CSNP::error(const string&amp; msg){
	cerr&lt;&lt;msg &lt;&lt;endl;
	exit(1);
}

 int main (int argc, char** argv){

	if(argv[1]==0)
		CSNP::error(&quot;No file exits. please give argv[1].&quot;)	;
	vector&lt;CSNP&gt; genInfo;
	CSNP SNP;
	ifstream file(argv[1]);
	while(file&gt;&gt;SNP)
	genInfo.push_back(SNP);	
	cout &lt;&lt;endl;
	cout&lt;&lt;&quot;Total number of SNPs read: &quot;&lt;&lt; genInfo.size()&lt;&lt;endl;
	cout &lt;&lt; &quot;---------Displaying SNP Info:----------------#\n&quot;;
	for (unsigned int i=0; i&lt;genInfo.size(); ++i)
	cout &lt;&lt; genInfo[i]&lt;&lt; '\n';
	cout &lt;&lt;endl;
 return 0;}
 ostream&amp; operator&lt;&lt;(ostream &amp;out, const CSNP &amp;pCSNP)
	  {

   return out &lt;&lt; &quot;chr No:&quot;&lt;&lt; pCSNP.nchr&lt;&lt; &quot;, SNP ID: &quot;&lt;&lt; pCSNP.snpName
	               &lt;&lt; &quot;, cm_pos: &quot; &lt;&lt; pCSNP.cm_pos&lt;&lt; &quot; and bp: &quot;&lt;&lt; pCSNP.bp;
	  }

 bool checkSNPinfo(const string&amp; line, const long int&amp; countSNPs){
	// This function will return true if there are four columns and if not then false:
	bool tfValue=false;
	//char* sline= (char*)line.c_str();
	string buffer;
	vector&lt;string&gt; snp;
	istringstream line_parser(line);
	if(line_parser.fail()){
		line_parser.unget();
		line_parser.clear(ios_base::failbit);	
		string count;
		stringstream ss;
		ss&lt;&lt;countSNPs;
		count=ss.str();
		string msg=&quot;There is problem in reading  &quot;+count +&quot;th line (excluding comments and blank lines) of your map file. Please correct or remove this line.\n&quot;;
		CSNP::error(msg);
	}
	if(line_parser.good())
		{
		//while(getline(line_parser,buffer)){
		while(line_parser&gt;&gt;buffer)
			snp.push_back(buffer);
			cout &lt;&lt; &quot;snp.size() : &quot; &lt;&lt;snp.size()&lt;&lt;endl;
			//	cout &lt;&lt; snp[0]&lt;&lt; &quot; &quot; &lt;&lt;snp[2]&lt;&lt;endl;

			if((snp.size()!=3) &amp;&amp; (snp.size()!=4))
				{
				string count;
				stringstream ss ;
				ss&lt;&lt;countSNPs;
				count=ss.str();
				string msg=&quot;It is expected to have either 3 or 4 columns in &quot;\
				+ count+\
				&quot;th line (excluding comments and blank lines) of your map file, but this is not the case here. Please either correct or delete this line. \n&quot;;
				CSNP::error(msg);
				}

		if(!three_columns &amp;&amp; (snp.size()==3))
			{
				three_columns=true;
			}
			else if(!three_columns &amp;&amp; snp.size()==4)
			{
				tfValue=true;

			}

			snp.resize(0);
		}

return tfValue; }

 istream&amp; operator &gt;&gt;(istream&amp; is, CSNP&amp; pCSNP){
	static vector&lt;bool&gt;bool_snpCol;// this will check if all rows of map  file has sam number of columns.
	string line;
	char cline[256];
    static long int countSNPs=1;
	is&gt;&gt;ws;
	while(getline(is, line, '\n') &amp;&amp; (!line.size() or line[0]=='#'));
	if(is.good()){
		bool tfValue=checkSNPinfo(line,countSNPs);
		if(bool_snpCol.empty())
		{	
		  bool_snpCol.push_back(tfValue);
		} 
		if(bool_snpCol.size()&lt;2)
		{

			bool_snpCol.push_back(tfValue);
			if(bool_snpCol[0]!= bool_snpCol[1])
			{
				string count;
				stringstream ss ;
				ss&lt;&lt;countSNPs;
				count=ss.str();
				string msg=&quot; The &quot; + count+&quot;th row (excluding comments and blank lines)&quot;+
				&quot;of your map file does not contain the same number of columns as in the previous rows. Please delete or correct this row.\n&quot;;
				CSNP::error(msg);
				}
		bool_snpCol.pop_back();		 	
		}

		stringstream line_parser(line);
		if(!tfValue) // four columns? If yes true;
			line_parser&gt;&gt;pCSNP.nchr&gt;&gt;pCSNP.snpName&gt;&gt;pCSNP.bp;
		else  
			line_parser&gt;&gt;pCSNP.nchr&gt;&gt;pCSNP.snpName&gt;&gt;pCSNP.cm_pos&gt;&gt;pCSNP.bp;

		if(line_parser.fail()){ 
			line_parser.clear(ios_base::failbit);
				string count;
				stringstream ss ;
				ss&lt;&lt;countSNPs;
				count=ss.str();
				string msg=&quot;There is a problem in &quot;\
				+ count+\
				&quot;th row of your map file. Please either correct it or delete it. &quot;;
				CSNP::error(msg);
			}

		is.exceptions(is.exceptions()|ios_base::badbit);
		if(line_parser.fail()){ 
		//line_parser.unget();
		line_parser.clear(ios_base::failbit);
		//return is; // end of file 
		}
	}
	if(is.eof()){ 
		//is.unget();
		is.clear(ios_base::failbit);
		return is; // end of file 
	}
	if(is.bad()) CSNP::error(&quot; istream is bad. FATAL ERROR!&quot;);
	//cout &lt;&lt; &quot;countSNPs: &quot;&lt;&lt; countSNPs &lt;&lt;endl;
 	countSNPs++;
 return is;}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/296148/vector-of-class-objects-vs-vector-of-pointers-to-class-objects</link><generator>RSS for Node</generator><lastBuildDate>Sat, 15 Aug 2026 04:58:21 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/296148.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 28 Nov 2011 21:51:21 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Mon, 28 Nov 2011 22:20:24 GMT]]></title><description><![CDATA[<p>Thanks to the two users &quot;SeppJ&quot; and &quot;Gugelmoser&quot;. With their help, I have written a complete program which can read a data file and check different errors. However, this program can't read large data file. When I read large data with like 100,00,000 lines, it tries nearly 4-5 min then it says something like bad alloc(). I do not know why? Could anyone please hint me why this program is inefficient? Any alternative program to read such data would be very appreciable.</p>
<p>The file looks as follows is as follows.</p>
<ul>
<li>having rows with usually a certain fixed number of columns,</li>
<li>different comments marked with the start letter #,</li>
<li>also contains some blank lines.</li>
<li>Blank lines and comment line should be ignored while reading.</li>
<li>The file may also contain by mistake wrong number of columns which should be corrected first as well.</li>
<li>Each row gives a class object and it should be saved as an element<br />
vector of class object.</li>
<li>the columns in each row may be either separated by both white space and by tabs mixed.</li>
</ul>
<p><strong>file content:</strong></p>
<pre><code>1 	snp1 		1.1 		1
2 snp2 1.2 2
3 snp3 1.3 3 
4 snp4 1.4 4 
# 1

#2
#hier
#3
#test
#

#
5 snp5 1.5 5 
#test
5 snp5 1.5 5 
2 test
6 snp6 1.5 6 
3ss  snp7
#test
#1
4 snp4 1.4 4 
5 snp5 1.5 5 
#
#

7 snp7 1.5 7
</code></pre>
<p>To read and display . I have written the following code.</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;
#include&lt;fstream&gt;
#include&lt;string&gt;
#include&lt;sstream&gt;
#include&lt;vector&gt;
#include&lt;algorithm&gt;
#include&lt;cstdlib&gt;

using namespace std;
 struct CSNP{
	friend istream&amp; operator&gt;&gt;(istream &amp; is, CSNP&amp; pCSNP);
	friend ostream&amp; operator&lt;&lt;(ostream &amp;out, const CSNP &amp;pCSNP);
	string nchr;
	string snpName;
	float cm_pos;
	long int bp;
	CSNP():nchr(&quot;&quot;),snpName(&quot;&quot;), cm_pos(0.0),bp(0){}
	vector&lt;CSNP&gt;lociInfo;
	static void readMapFile(const string&amp; fileName, vector&lt;CSNP&gt;locInfo);
	static void error(const string &amp; );
	static bool checkSNPinfo(const string&amp; line, const long int&amp; countSNPs);
	static bool three_columns;
		//~CSNP(); 
 };
 bool three_columns=false;
void CSNP::error(const string&amp; msg){
	cerr&lt;&lt;msg &lt;&lt;endl;
	exit(1);
}

 int main (int argc, char** argv){

	if(argv[1]==0)
		CSNP::error(&quot;No file exits. please give argv[1].&quot;)	;
	vector&lt;CSNP&gt; genInfo;
	CSNP SNP;
	ifstream file(argv[1]);
	while(file&gt;&gt;SNP)
	genInfo.push_back(SNP);	
	cout &lt;&lt;endl;
	cout&lt;&lt;&quot;Total number of SNPs read: &quot;&lt;&lt; genInfo.size()&lt;&lt;endl;
	cout &lt;&lt; &quot;---------Displaying SNP Info:----------------#\n&quot;;
	for (unsigned int i=0; i&lt;genInfo.size(); ++i)
	cout &lt;&lt; genInfo[i]&lt;&lt; '\n';
	cout &lt;&lt;endl;
 return 0;}
 ostream&amp; operator&lt;&lt;(ostream &amp;out, const CSNP &amp;pCSNP)
	  {

   return out &lt;&lt; &quot;chr No:&quot;&lt;&lt; pCSNP.nchr&lt;&lt; &quot;, SNP ID: &quot;&lt;&lt; pCSNP.snpName
	               &lt;&lt; &quot;, cm_pos: &quot; &lt;&lt; pCSNP.cm_pos&lt;&lt; &quot; and bp: &quot;&lt;&lt; pCSNP.bp;
	  }

 bool checkSNPinfo(const string&amp; line, const long int&amp; countSNPs){
	// This function will return true if there are four columns and if not then false:
	bool tfValue=false;
	//char* sline= (char*)line.c_str();
	string buffer;
	vector&lt;string&gt; snp;
	istringstream line_parser(line);
	if(line_parser.fail()){
		line_parser.unget();
		line_parser.clear(ios_base::failbit);	
		string count;
		stringstream ss;
		ss&lt;&lt;countSNPs;
		count=ss.str();
		string msg=&quot;There is problem in reading  &quot;+count +&quot;th line (excluding comments and blank lines) of your map file. Please correct or remove this line.\n&quot;;
		CSNP::error(msg);
	}
	if(line_parser.good())
		{
		//while(getline(line_parser,buffer)){
		while(line_parser&gt;&gt;buffer)
			snp.push_back(buffer);
			cout &lt;&lt; &quot;snp.size() : &quot; &lt;&lt;snp.size()&lt;&lt;endl;
			//	cout &lt;&lt; snp[0]&lt;&lt; &quot; &quot; &lt;&lt;snp[2]&lt;&lt;endl;

			if((snp.size()!=3) &amp;&amp; (snp.size()!=4))
				{
				string count;
				stringstream ss ;
				ss&lt;&lt;countSNPs;
				count=ss.str();
				string msg=&quot;It is expected to have either 3 or 4 columns in &quot;\
				+ count+\
				&quot;th line (excluding comments and blank lines) of your map file, but this is not the case here. Please either correct or delete this line. \n&quot;;
				CSNP::error(msg);
				}

		if(!three_columns &amp;&amp; (snp.size()==3))
			{
				three_columns=true;
			}
			else if(!three_columns &amp;&amp; snp.size()==4)
			{
				tfValue=true;

			}

			snp.resize(0);
		}

return tfValue; }

 istream&amp; operator &gt;&gt;(istream&amp; is, CSNP&amp; pCSNP){
	static vector&lt;bool&gt;bool_snpCol;// this will check if all rows of map  file has sam number of columns.
	string line;
	char cline[256];
    static long int countSNPs=1;
	is&gt;&gt;ws;
	while(getline(is, line, '\n') &amp;&amp; (!line.size() or line[0]=='#'));
	if(is.good()){
		bool tfValue=checkSNPinfo(line,countSNPs);
		if(bool_snpCol.empty())
		{	
		  bool_snpCol.push_back(tfValue);
		} 
		if(bool_snpCol.size()&lt;2)
		{

			bool_snpCol.push_back(tfValue);
			if(bool_snpCol[0]!= bool_snpCol[1])
			{
				string count;
				stringstream ss ;
				ss&lt;&lt;countSNPs;
				count=ss.str();
				string msg=&quot; The &quot; + count+&quot;th row (excluding comments and blank lines)&quot;+
				&quot;of your map file does not contain the same number of columns as in the previous rows. Please delete or correct this row.\n&quot;;
				CSNP::error(msg);
				}
		bool_snpCol.pop_back();		 	
		}

		stringstream line_parser(line);
		if(!tfValue) // four columns? If yes true;
			line_parser&gt;&gt;pCSNP.nchr&gt;&gt;pCSNP.snpName&gt;&gt;pCSNP.bp;
		else  
			line_parser&gt;&gt;pCSNP.nchr&gt;&gt;pCSNP.snpName&gt;&gt;pCSNP.cm_pos&gt;&gt;pCSNP.bp;

		if(line_parser.fail()){ 
			line_parser.clear(ios_base::failbit);
				string count;
				stringstream ss ;
				ss&lt;&lt;countSNPs;
				count=ss.str();
				string msg=&quot;There is a problem in &quot;\
				+ count+\
				&quot;th row of your map file. Please either correct it or delete it. &quot;;
				CSNP::error(msg);
			}

		is.exceptions(is.exceptions()|ios_base::badbit);
		if(line_parser.fail()){ 
		//line_parser.unget();
		line_parser.clear(ios_base::failbit);
		//return is; // end of file 
		}
	}
	if(is.eof()){ 
		//is.unget();
		is.clear(ios_base::failbit);
		return is; // end of file 
	}
	if(is.bad()) CSNP::error(&quot; istream is bad. FATAL ERROR!&quot;);
	//cout &lt;&lt; &quot;countSNPs: &quot;&lt;&lt; countSNPs &lt;&lt;endl;
 	countSNPs++;
 return is;}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2150261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150261</guid><dc:creator><![CDATA[euklid]]></dc:creator><pubDate>Mon, 28 Nov 2011 22:20:24 GMT</pubDate></item><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Mon, 28 Nov 2011 22:03:08 GMT]]></title><description><![CDATA[<p>please clean up the code and especially remove all comments which were placed by the guys to hint you what to do. also please choose variable names whcih are at least comprehensible, i.e more than 4 characters long.</p>
<p>At the moment, this is just abuse of the help offered by this forum.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150265</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Mon, 28 Nov 2011 22:03:08 GMT</pubDate></item><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Mon, 28 Nov 2011 22:08:51 GMT]]></title><description><![CDATA[<p>Without having read much of your code, if you try to read 100 million lines at a time, you run out of space, which is exactly what your error message says. If you want to handle very big files, you have to find a way to read them chunk by chunk. If you need arbitrary access to any line, think about reading the lines just in time you need them. The proxy pattern might help you.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150274</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150274</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 28 Nov 2011 22:08:51 GMT</pubDate></item><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Mon, 28 Nov 2011 22:22:00 GMT]]></title><description><![CDATA[<p>otze schrieb:</p>
<blockquote>
<p>please clean up the code and especially remove all comments which were placed by the guys to hint you what to do. also please choose variable names whcih are at least comprehensible, i.e more than 4 characters long.<br />
At the moment, this is just abuse of the help offered by this forum.</p>
</blockquote>
<p>thanks for your info. I haven't just copied and pasted the codes. I have worked my self and most of the comments were from me so that I can remember to work further on it.<br />
but according to your suggestions I have deleted. it.thank you.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150286</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150286</guid><dc:creator><![CDATA[euklid]]></dc:creator><pubDate>Mon, 28 Nov 2011 22:22:00 GMT</pubDate></item><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Mon, 28 Nov 2011 22:24:11 GMT]]></title><description><![CDATA[<p>Michael E. schrieb:</p>
<blockquote>
<p>Without having read much of your code, if you try to read 100 million lines at a time, you run out of space, which is exactly what your error message says. If you want to handle very big files, you have to find a way to read them chunk by chunk. If you need arbitrary access to any line, think about reading the lines just in time you need them. The proxy pattern might help you.</p>
</blockquote>
<p>That is exactly what I also think but till now I haven't got idea how to do that way. Would you please give me some Literature names or internet webs where I can read about it? By the way there were 1 million lines not 100.<br />
sorry<br />
thank you<br />
euklid</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150289</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150289</guid><dc:creator><![CDATA[euklid]]></dc:creator><pubDate>Mon, 28 Nov 2011 22:24:11 GMT</pubDate></item><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Mon, 28 Nov 2011 23:38:09 GMT]]></title><description><![CDATA[<p>How many memory is used by your program before the crash?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150309</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150309</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Mon, 28 Nov 2011 23:38:09 GMT</pubDate></item><item><title><![CDATA[Reply to vector of Class objects vs vector of pointers to class objects. on Tue, 29 Nov 2011 07:02:19 GMT]]></title><description><![CDATA[<p>euklid schrieb:</p>
<blockquote>
<p>thanks for your info. I haven't just copied and pasted the codes. I have worked my self and most of the comments were from me so that I can remember to work further on it.<br />
but according to your suggestions I have deleted. it.thank you.</p>
</blockquote>
<p>the intendation is still terrible misleading and makes the code hard to understand. Braces seem to be placed totally random. It looks more like copy &amp; paste than real programmers work. This chaos makes it really hard to understand.</p>
<p>do you understand it?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2150348</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2150348</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Tue, 29 Nov 2011 07:02:19 GMT</pubDate></item></channel></rss>