[英]How to trim all the preceding zeroes of a 32-bit binary number?
I was trying to trim all the leading/preceding zeroes in a binary 32-bit number and print out the output by these following 2 codes of which the first one showed error while the other code was successful:我试图修剪二进制 32 位数字中的所有前导/前导零,并通过以下 2 个代码打印输出,其中第一个代码显示错误,而另一个代码成功:
I want to know why the former one failed.我想知道前一个失败的原因。 please explain in detail.请详细解释。 it would mean a lot to me.这对我来说意义重大。
string trimmer(string& binary_str) {
int i=0;// 'i' will finally have the index of the binary string which is having value as '0' and is
// just the preceding zero to the first 1 in the string
while (binary_str.at(i) != 1) {//this will iterate over the srting until it reaches the zero before
//the first 1 in the string
i++;
}
binary_str.erase(0, i); //this will erase all the preceeding zeroes
return binary_str;
}
This above written code didnt work and showed up error,上面写的代码不起作用并出现错误,
But this second code worked:但是这第二个代码有效:
string trimmer(string& binary_str) {
int i=0;
for (i = 0; i < 32; i++) {
if (binary_str.at(i) == '1') {
break;
}
}
binary_str.erase(0, i);
return binary_str;
}
Both had the function, ie, if input was given: a binary string 00000000000000000000001011001011 then the output should be 1011001011.两者都有函数,即,如果给出输入:一个二进制字符串 00000000000000000000001011001011 那么输出应该是 1011001011。
The former code used while loop whereas the latter used for loop with if conditional.前一个代码使用 while 循环,而后者使用 if 条件循环。 bot codes follow the same logic.机器人代码遵循相同的逻辑。 but the former one with while loop showed up error whereas the latter one didn't.但是前一个带有while循环的出现错误而后一个没有。
Can anybody explain to me the reason behind the while loop to show up error?任何人都可以向我解释 while 循环背后显示错误的原因吗? it would mean a lot to me.这对我来说意义重大。
The complete code is as follows:完整代码如下:
#include<bits/stdc++.h>
using namespace std;
/*
string trimmer(string& binary_str) {
int i=0;
while (binary_str.at(i) != 1) {
i++;
}
binary_str.erase(0, i);
return binary_str;
}
*/
string trimmer(string& binary_str) {
int i=0;
for (i = 0; i < 32; i++) {
if (binary_str.at(i) == '1') {
break;
}
}
binary_str.erase(0, i);
return binary_str;
}
int main(){
string bStr="00000000001011001011";
bStr = trimmer(bStr);
cout<< bStr<<'\n';
return 0;
}
You can use find_first_not_of
to find the first character of that isn't a 0
.您可以使用find_first_not_of
来查找不是0
的第一个字符。 Then you can erase
all characters up to that point.然后您可以erase
到该点的所有字符。 If a string is all zeros, it will be completely erased.如果一个字符串全为零,它将被完全擦除。
void trim_leading_zeros(std::string &str) {
str.erase(0, str.find_first_not_of('0'));
}
Try using modern C++ features:尝试使用现代 C++ 功能:
#include <iostream>
#include <algorithm>
#include <string>
void ltrim_zero(std::string& str) {
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int character) {
return '0' != character;
}));
}
int main(){
std::string bStr = "00000000001011001011";
ltrim_zero(bStr);
std::cout << bStr << std::endl;
return 0;
}
find the first occurrence of '1' and use this variation of string constructor找到第一次出现的 '1' 并使用这个字符串构造函数的变体
string (const string& str, size_t pos, size_t len = npos);
str=string to be copied from str=要复制的字符串
pos= starting position to copy from pos=要复制的起始位置
npos= number of character to copy from source(str) string. npos= 要从源(str)字符串复制的字符数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.