Posted on 2008-10-14 20:05
美洲豹 閱讀(631)
評論(0) 編輯 收藏 引用
//原理
一個個從前往后找,找到第一個不是數字的字符位置用npos表示,然后
string::substr(0,npos)提取出一個字符串用atoi轉換存儲一個變量,npos位置上的
字符常量存儲到另一個,然后對原字符串進行 string::erase(0,npos+1)操作得到剩下字
符串,繼續進行以上操作,就可以了,只是到最后稍做改動就OK了。
//例子
功能:
給定形如“127.0.0.1|1024”的字符串,提取出IP地址字符串和整型端口號。
其中IP和端口號分隔符“|”可替換,默認為“:”,但不可為“.”。
代碼如下(VC6+WinXP調試通過):
#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);