问题:读取ip比如“192.168.1.2”,提取出里面各个字段的数字。
解决:循环中,首先cin>>a (a是一个int),然后cin>>s (s是一个char)。这样可以避免先读取string然后再转换为int的麻烦。
#include<iostream>
#include<cstdio>using namespace std;
int main(){
int a;
char s='.';
while(s=='.'){
cin>>a;
cout<<a<<endl;
s=getchar();
}
return 0;
}
输出结果:
192.168.1.2
192
168
1
2
本文介绍了一种使用C++从IP地址中提取数字字段的方法。通过循环读取整数和分隔符,有效地解析了如'192.168.1.2'这样的IP地址。
3790

被折叠的 条评论
为什么被折叠?



