Problem with STL vecctor algorithm count



  • Hello! Could any one help me please? I have used count in my bool vector but it is not counting my bools properly.
    The template of count is :

    template <class InputIterator, class T>
      typename iterator_traits<InputIterator>::difference_type
        count ( ForwardIterator first, ForwardIterator last, const T& value );
    

    My program:

    // using in count in vector.
    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main (){
    	int c;
    	vector<bool> bv(true,4);
    	cout <<"bool vector:\n";
    	for (int i=0;i<4;i++)cout << " " << bv[i] ; 
    	cout <<endl;
    	c =(int) count(bv.begin(),bv.end(),true);
    	cout<<"total counts of true: "<< c <<endl;
    	c= (int) count(bv.begin(),bv.end(),false);
    	cout<<"total counts of false: "<< c <<endl;
    	//changing the value of bv Vector 
    	bv[2] = false;
    	// cout 
    	cout <<"bool vector:\n";
    	for (int i=0;i<4;i++)cout << " " << bv[i] ; 
    	cout << endl;
    	c=(int)count(bv.begin(), bv.end(), true);
    	cout << "\n toal  trues : "<< c <<endl;
    	c=(int)count(bv.begin(), bv.end(), false);
    	cout << "\n toal falses : "<< c <<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
    */
    


  • Actually, count is working correctly. You need to take a closer look at the vector constructor you're using:

    explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
    

    You're creating a vector which has just one element (because (int)true == 1).



  • dooooomi schrieb:

    Actually, count is working correctly. You need to take a closer look at the vector constructor you're using:

    explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
    

    You're creating a vector which has just one element (because (int)true == 1).

    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
    how I can get the indices of elements of a vector which has vlaue false.?
    Every help is very much appreciated.



  • So what now? Do you want to count the number of true/false values or find their indices?

    Anyway, you're constructing the vector wrong, as dooooomi said. You confused the two parameters.
    You can think of bools as specific ints. 0 = false, all other numbers = true.

    vector<bool> bv(true, 4);
    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, "true" 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.
    That means, your constructor is the same as:
    vector<bool> bv(1, true);



  • Thank you so much you two guys. I has been clear now!


Anmelden zum Antworten