<?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[Problem with STL vecctor algorithm count]]></title><description><![CDATA[<p>Hello! Could any one help me please? I have used count in my bool vector but it is not counting my bools properly.<br />
The template of count is :</p>
<pre><code class="language-cpp">template &lt;class InputIterator, class T&gt;
  typename iterator_traits&lt;InputIterator&gt;::difference_type
    count ( ForwardIterator first, ForwardIterator last, const T&amp; value );
</code></pre>
<p>My program:</p>
<pre><code class="language-cpp">// using in count in vector.
#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;string&gt;
#include&lt;algorithm&gt;
using namespace std;
int main (){
	int c;
	vector&lt;bool&gt; bv(true,4);
	cout &lt;&lt;&quot;bool vector:\n&quot;;
	for (int i=0;i&lt;4;i++)cout &lt;&lt; &quot; &quot; &lt;&lt; bv[i] ; 
	cout &lt;&lt;endl;
	c =(int) count(bv.begin(),bv.end(),true);
	cout&lt;&lt;&quot;total counts of true: &quot;&lt;&lt; c &lt;&lt;endl;
	c= (int) count(bv.begin(),bv.end(),false);
	cout&lt;&lt;&quot;total counts of false: &quot;&lt;&lt; c &lt;&lt;endl;
	//changing the value of bv Vector 
	bv[2] = false;
	// cout 
	cout &lt;&lt;&quot;bool vector:\n&quot;;
	for (int i=0;i&lt;4;i++)cout &lt;&lt; &quot; &quot; &lt;&lt; bv[i] ; 
	cout &lt;&lt; endl;
	c=(int)count(bv.begin(), bv.end(), true);
	cout &lt;&lt; &quot;\n toal  trues : &quot;&lt;&lt; c &lt;&lt;endl;
	c=(int)count(bv.begin(), bv.end(), false);
	cout &lt;&lt; &quot;\n toal falses : &quot;&lt;&lt; c &lt;&lt;endl;
return 0;	
}
//The result should be 
/*
bool vector:
 1 1 1 1
total counts of true: 4
total counts of false: 0
bool vector:
 1 1 0 1
toal  trues : 3

toal falses : 1

*/
// However the excution of the program is giving me  : 
/*
bool vector:
 1 1 1 1
total counts of true: 1
total counts of false: 0
bool vector:
 1 1 0 1
toal  trues : 1
toal falses : 0
*/
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/295075/problem-with-stl-vecctor-algorithm-count</link><generator>RSS for Node</generator><lastBuildDate>Sat, 15 Aug 2026 15:11:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/295075.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Nov 2011 20:02:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem with STL vecctor algorithm count on Sun, 06 Nov 2011 20:02:30 GMT]]></title><description><![CDATA[<p>Hello! Could any one help me please? I have used count in my bool vector but it is not counting my bools properly.<br />
The template of count is :</p>
<pre><code class="language-cpp">template &lt;class InputIterator, class T&gt;
  typename iterator_traits&lt;InputIterator&gt;::difference_type
    count ( ForwardIterator first, ForwardIterator last, const T&amp; value );
</code></pre>
<p>My program:</p>
<pre><code class="language-cpp">// using in count in vector.
#include&lt;iostream&gt;
#include&lt;vector&gt;
#include&lt;string&gt;
#include&lt;algorithm&gt;
using namespace std;
int main (){
	int c;
	vector&lt;bool&gt; bv(true,4);
	cout &lt;&lt;&quot;bool vector:\n&quot;;
	for (int i=0;i&lt;4;i++)cout &lt;&lt; &quot; &quot; &lt;&lt; bv[i] ; 
	cout &lt;&lt;endl;
	c =(int) count(bv.begin(),bv.end(),true);
	cout&lt;&lt;&quot;total counts of true: &quot;&lt;&lt; c &lt;&lt;endl;
	c= (int) count(bv.begin(),bv.end(),false);
	cout&lt;&lt;&quot;total counts of false: &quot;&lt;&lt; c &lt;&lt;endl;
	//changing the value of bv Vector 
	bv[2] = false;
	// cout 
	cout &lt;&lt;&quot;bool vector:\n&quot;;
	for (int i=0;i&lt;4;i++)cout &lt;&lt; &quot; &quot; &lt;&lt; bv[i] ; 
	cout &lt;&lt; endl;
	c=(int)count(bv.begin(), bv.end(), true);
	cout &lt;&lt; &quot;\n toal  trues : &quot;&lt;&lt; c &lt;&lt;endl;
	c=(int)count(bv.begin(), bv.end(), false);
	cout &lt;&lt; &quot;\n toal falses : &quot;&lt;&lt; c &lt;&lt;endl;
return 0;	
}
//The result should be 
/*
bool vector:
 1 1 1 1
total counts of true: 4
total counts of false: 0
bool vector:
 1 1 0 1
toal  trues : 3

toal falses : 1

*/
// However the excution of the program is giving me  : 
/*
bool vector:
 1 1 1 1
total counts of true: 1
total counts of false: 0
bool vector:
 1 1 0 1
toal  trues : 1
toal falses : 0
*/
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2141355</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2141355</guid><dc:creator><![CDATA[euklid]]></dc:creator><pubDate>Sun, 06 Nov 2011 20:02:30 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with STL vecctor algorithm count on Sun, 06 Nov 2011 20:27:47 GMT]]></title><description><![CDATA[<p>Actually, <code>count</code> is working correctly. You need to take a closer look at the <code>vector</code> constructor you're using:</p>
<pre><code>explicit vector ( size_type n, const T&amp; value= T(), const Allocator&amp; = Allocator() );
</code></pre>
<p>You're creating a vector which has just one element (because (int)true == 1).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2141365</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2141365</guid><dc:creator><![CDATA[dooooomi]]></dc:creator><pubDate>Sun, 06 Nov 2011 20:27:47 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with STL vecctor algorithm count on Sun, 06 Nov 2011 21:14:33 GMT]]></title><description><![CDATA[<p>dooooomi schrieb:</p>
<blockquote>
<p>Actually, <code>count</code> is working correctly. You need to take a closer look at the <code>vector</code> constructor you're using:</p>
<pre><code>explicit vector ( size_type n, const T&amp; value= T(), const Allocator&amp; = Allocator() );
</code></pre>
<p>You're creating a vector which has just one element (because (int)true == 1).</p>
</blockquote>
<p>Actually, I want to find indices of falses in a large vector with values truth and false. I tried many times but it is not working. could you please explain me<br />
how I can get the indices of elements of a vector which has vlaue false.?<br />
Every help is very much appreciated.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2141384</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2141384</guid><dc:creator><![CDATA[euklid]]></dc:creator><pubDate>Sun, 06 Nov 2011 21:14:33 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with STL vecctor algorithm count on Sun, 06 Nov 2011 22:17:10 GMT]]></title><description><![CDATA[<p>So what now? Do you want to count the number of true/false values or find their indices?</p>
<p>Anyway, you're constructing the vector wrong, as dooooomi said. You confused the two parameters.<br />
You can think of bools as specific ints. 0 = false, all other numbers = true.</p>
<p>vector&lt;bool&gt; bv(true, 4);<br />
true is the size of the vector and 4 is the value which is used to fill that size. Because the first argument is an int, &quot;true&quot; is converted which is usually 1. The second argument is the element type, i.e. bool, and thus is converted as well. 4 is not zero, so it's true.<br />
That means, your constructor is the same as:<br />
vector&lt;bool&gt; bv(1, true);</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2141404</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2141404</guid><dc:creator><![CDATA[Wurstinator]]></dc:creator><pubDate>Sun, 06 Nov 2011 22:17:10 GMT</pubDate></item><item><title><![CDATA[Reply to Problem with STL vecctor algorithm count on Sun, 06 Nov 2011 23:47:44 GMT]]></title><description><![CDATA[<p>Thank you so much you two guys. I has been clear now!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2141422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2141422</guid><dc:creator><![CDATA[euklid]]></dc:creator><pubDate>Sun, 06 Nov 2011 23:47:44 GMT</pubDate></item></channel></rss>