2010-11-19 6 views
0

다음 코드가 있습니다.boost :: algorithm :: split을 사용하여 문자열을 분할하십시오.

using namespace std; 
using namespace boost; 
int main() 
{ 
SystemConnect hndl; 
int ip1[15],ip2[15]; 
string line; 
while (cout<<"LP>" && getline(cin,line)) { 
    if (line=="exit") 
    break; 
    if (line=="Connect 10.172.21.121 10.109.12.122"){ 
    string str; 
     str="ConInit 10.172.21.121 10.109.12.122"; 
    vector<string> results; 
    split(results,str,is_any_of(" ")); 
    for(vector<string>::const_iterator p=results.begin();p!=results.end();p++){ 
    cout<<*p<<endl; 
    } 
    } 
} 
} 

이것은 내가 얻는 결과입니다.

Connect 
10.172.21.121 
10.109.12.122 

나는 IP2에서 IP1 & 10.109.12.122에서 10.172.21.121을 저장해야합니다. 내가 어떻게

감사

+0

왜 ip1과 ip2가 int []로 정의되어 있습니까? 어떤 형태로 IP 주소를 저장하길 기대합니까? –

+2

왜 IP 주소를 저장하기 위해 15- 요소 int 배열이 필요하다고 생각하십니까? –

답변

23

를 이미 부스트를 사용하는 경우, 왜 적절한 클래스의 객체에 IP 주소를 저장하지 않도록해야합니까?

#include <iostream> 
#include <vector> 
#include <string> 
#include <boost/algorithm/string.hpp> 
#include <boost/asio.hpp> 
namespace ip = boost::asio::ip; 
int main() 
{ 
    std::string str = "ConInit 10.172.21.121 10.109.12.122"; 
    std::vector<std::string> results; 
    boost::split(results, str, boost::is_any_of(" ")); 
    ip::address ip1 = ip::address::from_string(results[1]); 
    ip::address ip2 = ip::address::from_string(results[2]); 
    std::cout << " ip1 = " << ip1 << " ip2 = " << ip2 << '\n'; 
} 

정수로 변환해야하는 경우 필요에 따라 to_bytes과 같이 입력하면됩니다.

관련 문제