0

When I am using str[] I get compilation error of mismatch type.When I use str+push_back instead of str[] everything works fine.

   class Solution {
public:
   string bits(int n){
       string str[32];//size already known thats why used str[32]
        for(long long int i=31;i>=0;i--)
        {
            if(n & 1<<i){
                str[i]=('1');
            }else{
                str[i]=('0');
            }
            
        }
       return str;
    };
    int findMaximumXOR(vector<int>& nums) {
        queue<int>q;
        vector<string>s(nums.size());
        for(int i=0;i<nums.size();i++ ){
           s[i]=bits(nums[i]);
            cout<<s[i]<<"\n";
        }
        return 0;
    }
};
2
  • 6
    string str[32]; is an array of 32 strings named str. It's not a string with 32 characters. std::string handles memory management for you. Just declare a std::string str; and call it a day. Alternative constructors here if you want to create it with a certain size. Commented Sep 30, 2020 at 1:06
  • In this case str[i] refers to string #i in that array of strings. You end up trying to assign a char to a std::string. Commented Sep 30, 2020 at 1:17

2 Answers 2

4

string str[32]; is declaring an array of 32 std::string objects. But the bits() method is declared as returning a single std::string, which is why return str; does not work.

You don't want an array of 32 strings. You want a single string of 32 characters in length, eg:

std::string bits(int n) {
    std::string str(32, '0');
    for(long long int i = 31; i >= 0; i--) {
        if (n & (1 << i)) {
            str[i] = '1';
        }
    }
    return str;
}

That being said, As @Slava mentioned in comments, if you want a better way, use std::bitset instead:

std::string bits(int n) {
    return std::bitset<32>(n).to_string();
}
Sign up to request clarification or add additional context in comments.

8 Comments

A string filled with NUL?
@tadman if you want to allocate up front in one line you have to fill it with something. OP has a comment about setting the array to 32 because they know how many characters they need. Looks like a leethaxxorchef.ru website thing so performance matters.
If performance matters, fill it with '0' so you don't need to do anything for those. On average 50% less work!
Seems like wasted effort. You know it's going to be '1' or '0', there's no ambiguity there, so pick one and pre-fill it. Then just skip those during the iteration.
@tadman actually if str is filled with '0' it allows: for(int i = 31; n; i--, n >>= 1 ) str[i] = (n & 1) + '0'; :)
|
3

If you want a string representing a 32-bit binary expression you need a single string. In C++ [] in a declaration generally refers to an array of something, as in int x[N] is an array of N int values.

Reworked:

  string bits(int n) {
    std::string str(32, '0'); // Reserve with 32 x 0

    for(long long int i=31;i>=0;i--) {
      if(n & 1<<i) {
          str[i]=('1');
      }
    }

    return str;
  };

There's a lot of quirks in this code, too, which I'll remove here:

  // Avoid using namespace std; and embrace the std:: prefix
  std::string bits(int n) {
    string str(32, '0');

    // You do not need a long long int to iterate to 31
    // Also try and express your loops as going from 0..N-1 unless
    // doing so overly complicates the code.
    for (int i=0; i<32; ++i) {
      // Although not necessary, the brackets around (1 << i)
      // make it abundantly clear this is not to be read as
      // something like (n & 1) << i
      if (n & (1 << i)) {
        // The braces around the '1' serve no purpose
        str[i]='1';
      }
    }

    return str;
  };

4 Comments

Or simply one line: return std::bitset<32>( n ).to_string();
@Slava Yeah, if doing so is permitted. This looks like an assignment.
OP is asking for the best way, nothing about restrictions
@Slava The Solution name of the class suggests to me it's an assignment, but your point is valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.