該程序用于去除字符串開頭的零字符。當輸入"0000123456"時,程序會輸出"123456"。核心函數removeZero()通過while循環找到第一個非零字符的位置,然后使用erase()方法刪除前面的所有零。主函數讀取輸入字符串并調用該函數處理。程序簡潔高效,適用于需要去除前導零的場景。
輸入
0000123456
輸出
123456
#include<bits/stdc++.h>
using namespace std;
string removeZero(string str){int i=0;while(str[i]=='0'){i++;}str.erase(0,i);return str;
}
int main(){string str;cin>>str;cout<<removeZero(str);return 0;
}