-2

here is the code :

#include <iostream>
#include <ostream>
#include <bits/stdc++.h>
#include <string>
int main(){
    std::string num {"hello"};
    std::string num1 {"hii"};
    int  nums = std::count(num.begin(),num.end(),num[0]);
    int  nums1 = std::count(num1.begin(),num1.end(),num1[0]);
    std::cout << std::count(num1.begin(),num1.end(),num1[0] ) == count(num.begin(),num.end(),num[0]);
    std::cout << nums == nums1 ;
    return 0;
}

please help if anyone know problem. updated......

  • 1
    What *is* the problem with the code you show? What happens? What should happen? What is the expected and actual output? And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your questions to improve them (like actually *asking* a question). – Some programmer dude Jul 25 '21 at 10:57
  • 1
    And have you tried to print the actual results of the `count` calls? And does the shown code even build? That's not how one use `std::count_if`. Perhaps that *is* the problem you want to ask about? Then please [edit] your question to actually say that. And include the full and complete build output (copy-pasted *as text*). – Some programmer dude Jul 25 '21 at 11:00
  • Why are you using `std::count_if`? What is it supposed to do? Why are you trying to compute `nums` and `nums1`? Where do you need these values? Why are you using `##include `? Why do you need `#include `? Why are you using `count` both with and without `std::`? – n. m. could be an AI Jul 25 '21 at 11:01
  • You solved *one problem, but we don't know if that problem was what you were asking about or not. Please don't update your questions to "fix" problems posted in comments and answers unless they happened to be typos that aren't part of the actual problem you're asking about. And you *still* haven't actually asked us a question, or told us what you want us to help you with. What is the problem with the code you show? – Some programmer dude Jul 25 '21 at 12:12
  • By the way, [never include that `` header](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). That's a bad and nasty habit. – Some programmer dude Jul 25 '21 at 12:12

1 Answers1

1

To use == in operator << we need to add parentheses here since overloaded operator << has higher precedence than comparison operator. If you use a mordern compiler, it will give you detailed hints, such as clang 11 will give use such warnings:

warning: overloaded operator << has higher precedence than comparison operator [-Woverloaded-shift-op-parentheses] 

Then we can fix it as:

#include <algorithm>
#include <iostream>
#include <ostream>
#include <string>
int main() {
  std::string num{"hello"};
  std::string num1{"hii"};
  std::size_t nums = std::count(num.begin(), num.end(), num[0]);
  std::size_t nums1 = std::count(num1.begin(), num1.end(), num1[0]);
  std::cout << (std::count(num1.begin(), num1.end(), num1[0]) ==
                count(num.begin(), num.end(), num[0]));
  std::cout << (nums == nums1);
  return 0;
}

Online demo

prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42