-4

I am getting stuck when using this line and not able to understand.

Can Anybody tell me what is the mean of

while(i < static_cast<int>(str.size() - 1))

in the Following Code?

#include <iostream>

using namespace std;

int main() {
    string str;
    cin >> str;
    int i = 0;

    while(i < static_cast<int>(str.size() - 1)) {
        if(i > -1 && str[i] == str[i + 1]) {
            str.erase(i,2);
            i--;
        } 
        else {
            i++;
        }
    }

    if(str.empty())
       cout << "Empty String" << endl;
    else
       cout << str << endl;

    return 0;
}
Akira
  • 4,385
  • 3
  • 24
  • 46
  • Possible duplicate of [When should static\_cast, dynamic\_cast, const\_cast and reinterpret\_cast be used?](https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used) –  Jul 26 '17 at 05:34
  • 8
    You *do* know about `while` loops? Do you know what [`str.size()`](http://en.cppreference.com/w/cpp/string/basic_string/size) returns? Do you know about [`static_cast`](http://en.cppreference.com/w/cpp/language/static_cast)? If you know about all these things separately, then just put that knowledge together. – Some programmer dude Jul 26 '17 at 05:34
  • Do you know what a cast is ? – Cedias Jul 26 '17 at 06:04
  • Some programmer dude ya i know loops but tell me what this "static_cast< int > (str.size()-1) " doing in this code – dreamstracker Jul 26 '17 at 06:11
  • 1
    This code is awful . Someone probably wrote the cast to silence a compiler warning but they broke the code in the process – M.M Jul 26 '17 at 06:16
  • @dreamstracker, don't forget to accept the answer you think that answered your question to help future users finding most accurate one for the same question. – Akira Aug 02 '17 at 06:31

3 Answers3

0

str.size(): take the size of a string. return type is size_t
str.size()-1: take the size minus 1. still size_t
static_cast< int > (str.size()-1): cast the size_t to an int. so trim overflow binary digits if sizeof(size_t) > sizeof(int) or left pad with zeros if sizeof(size_t) < sizeof(int), and treat as signed integer rather than unsigned.

Yuval Ben-Arie
  • 1,280
  • 9
  • 14
  • tell me in non technical way just give simple answer what they are doing ?? – dreamstracker Jul 26 '17 at 06:09
  • 2
    The behaviour in case of "overflow digits" is implementation-defined. Not safe to rely on "trim" – M.M Jul 26 '17 at 06:13
  • @dreamstracker: "tell me in a non-technical way" - you're learning programming, which is absolutely a technical skill. You better get used to that. – MSalters Jul 26 '17 at 14:51
0

If you compare a signed and an unsigned integer, the compiler would warn you for that. E.g. would show the following warning message:

warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

The std::string::size returns an unsigned integer, an std::size_t which is an implementation-defined type and it can store the maximum size of a theoretically possible object of any type.

If you want to suppress these warnings, you have to cast the signed integer to unsigned before comparison. The code snippet you questioning does the opposite: casts (with static_cast) the std::size_t (unsigned) type to int (signed) type. This is a bad practice because if the str.size() - 1 is higher than INT_MAX, it will produce an implementation-defined behaviour.

Akira
  • 4,385
  • 3
  • 24
  • 46
0

What it does?

It takes str's size, and subtracts 1 from it. Then, it converts the resulting number to int. Then it compares this number to i.

Why does it convert it? If it didn't do it, an unsigned number would be compared to a signed one. There are rules for this case in the standard, but they are complex, and depend on the sizeof's of int and size_t. You'd better not compare a signed number to an unsigned. A compiler usually warns you about it.

How could it be a problem? Consider, what if str.size() returns 0. Then, applying -1 on it, it becomes a huge number, SIZE_MAX. So this loop wouldn't work as intended (it would loop a lot of times instead of skipping the loop entirely, so it would crash), if the cast wouldn't be there.

Note that this code has a potential bug of using int here. It's because, if str.size() contains a number, which doesn't fit into an int, the program will misbehave. It should have used size_t for i, and use while (i+1<str.size()) instead.

To me, the decision of using unsigned numbers for size() methods, is one of the design mistakes of the standard library (I know that a lot of people think differently, but this is my opinion).

geza
  • 28,403
  • 6
  • 61
  • 135