Posted on 2008-10-14 20:05
美洲豹 閱讀(630)
評(píng)論(0) 編輯 收藏 引用
//原理
一個(gè)個(gè)從前往后找,找到第一個(gè)不是數(shù)字的字符位置用npos表示,然后
string::substr(0,npos)提取出一個(gè)字符串用atoi轉(zhuǎn)換存儲(chǔ)一個(gè)變量,npos位置上的
字符常量存儲(chǔ)到另一個(gè),然后對(duì)原字符串進(jìn)行 string::erase(0,npos+1)操作得到剩下字
符串,繼續(xù)進(jìn)行以上操作,就可以了,只是到最后稍做改動(dòng)就OK了。
//例子
功能:
給定形如“127.0.0.1|1024”的字符串,提取出IP地址字符串和整型端口號(hào)。
其中IP和端口號(hào)分隔符“|”可替換,默認(rèn)為“:”,但不可為“.”。
代碼如下(VC6+WinXP調(diào)試通過(guò)):
#include <iostream>
#include <string>
using namespace std;
void getIP( const string fullIP, string& IP, unsigned int& port, const char
dot = ':' )
{
IP = fullIP.substr( 0, fullIP.find_first_of(dot) );
port = atoi( fullIP.substr( fullIP.find_first_of(dot)+1, fullIP.length()
).c_str() );
}
void main()
{
string ip;
unsigned int port = 0;
char abc[] = "127.0.0.1:1024";
getIP( (string)abc, ip, port );
cout << ip << " " << port << endl;
string efg = "192.168.0.1|80";
getIP( efg, ip, port, '|' );
cout << ip << " " << port << endl;
}
pow((int)2,(int)3);
pow((double)2,(double)3);
pow(float,float);
pow(2,3);