The following code should remove the last char of the string and add l (lowercase L) if flip is true or r if it is false.
l
flip
r
std::stringstream ss; ss << code.substr(0, code.size() - 1); ss << flip ? "l" : "r"; std::string _code = ss.str();
However, when flip true, it adds 1 , and when it is false, it adds 0 . Why?
1
0
Priority issue.
ss << flip ? "l" : "r";
means
(ss << flip) ? "l" : "r";
Using
ss << ( flip ? "l" : "r" );
This is due to the priority of the operator. << has priority over ? , which means flip added first on ss .
<<
?
ss
The following should lead to the expected behavior:
ss << (flip ? "l" : "r");
Source: https://habr.com/ru/post/1210675/More articles:The Paste Bind Data parameter is required by the Kendo Grid Error any - asp.net-mvc-3How to start a single deployment upon successful completion of the Travis build? - pythonWatchKit Menu Items Not Displayed - watchkitNaive Bayes text classification algorithm - javaCSS class attribute data binding in XML view - data-binding"require" was used before its definition - javascriptHow to dynamically load an XML fragment as XML? - sapui5nock.js - how to match any combination of parameters in a url - javascriptBinding to the class attribute in the control - sapui5D3 events triggering svg hidden element - javascriptAll Articles