casting - C++ static_cast< int > (str.size()-1) means? -
i getting stuck when using line , not able understand.
can tell me mean of
while(i < static_cast<int>(str.size() - 1))
in following code?
#include <iostream> using namespace std; int main() { string str; cin >> str; int = 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; }
if compare signed , unsigned integer, compiler warn that. e.g. gcc show following warning message:
warning: comparison between signed , unsigned integer expressions [-wsign-compare]
the std::string::size
returns unsigned integer, std::size_t
implementation-defined type , can store maximum size of theoretically possible object of type.
if want suppress these warnings, have cast signed integer unsigned before comparison. code snippet questioning opposite: casts (with static_cast
) std::size_t
(unsigned) type int
(signed) type. this bad practice because if str.size() - 1
higher int_max
, produce implementation-defined behaviour.
Comments
Post a Comment