atol
通過,直到出現一個非數字看到數字
整個字符串不是數字,你會從這個代碼
int main()
{
long Id1;
std::string item = "1.2.3.4.5.6.7.8.9.10";
std::cout << item << std::endl;
}
UPD看到整個字符串:
我建議我的有效和通用算法:
PARAMS:
str - string to parse
sep - fiels separator, which delimit digit position
textbase - radix of digits in text string
fieldbase - field radix of position in target number
看到這裏基數:https://en.wikipedia.org/wiki/Radix和
template<typename T>
T str2num(const std::string &str, char sep, int textbase, int fieldbase) {
T digit, result = 0;
std::size_t pos=0, start = 0, end = str.size() - 1;
std::string strnum;
while((start = str.rfind(sep,end)) != std::string::npos) {
strnum = str.substr(start + 1, end - start);
digit = std::strtol(strnum.c_str(), 0, textbase);
result += digit * pow(fieldbase, pos);
end = start - 1;
++pos;
}
strnum = str.substr(0, end + (start ? 1 : 0));
digit = std::strtol(strnum.c_str(), 0, textbase);
result += digit * pow(fieldbase, pos);
return result;
}
因此可用於任何目的,類比這種普遍的功能。例如解析並將IP地址從字符串轉換爲二進制形式。目前對於IPv4來說很簡單。但是,當編譯器支持128位寬度的整數值時,IPv6將變得簡單。但是現在的IPv6可以用分割字符串解析爲兩個帶有四個字段的子字符串。
有一個例子如何使用:
int main()
{
typedef unsigned long ul32;
typedef unsigned long long ul64;
// parse the IPv4 adress, where numerical system of digits
// in fields id 10, and field radix is 256 (in hex 0x100)
ul32 x32 = str2num<ul32>("10.1.10.127", '.', 10, 256);
std::cout << x32 << std::endl;
// parse half of the IPv6 adress, where numerical system of digits
// in fields id 16, and field radix is 65536 (in hex 0x10000)
ul64 x64 = str2num<ul64>("1234:5678:9abc:def0", ':', 16, 65536);
std::cout << std::hex << x64 << std::endl;
return 0;
}
結果輸出:
167840383
123456789abcdef0
PS:
時,你會嘗試用64位整數編譯這一點,非標準庫pow()函數不支持64位值,所以需要使用pow的特殊實現。例如印度pow算法。我發現這裏的Pascal實現:http://www.algolib.narod.ru/Math/IndianPow.html所以要轉換成C++:
template<typename T>
T pow(T base, unsigned int exp) {
T t = base, res = 1;
while(1) {
if (exp & 1) res *= t;
exp >>= 1;
if (! exp) return res;
t *= t;
}
}
這是您編寫的代碼的正確輸出。你想要什麼結果? –
除非你需要額外的東西,否則不要使用'std :: endl'。 ''\ n''開始一個新行。 –
確實得到10! ?處理「。」作爲多個操作? –