使用boost分割字符串::算法::分裂 [英] Splitting the string using boost::algorithm::split

查看:620
本文介绍了使用boost分割字符串::算法::分裂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code。

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

我需要存储在IP1&放10.172.21.121; 10.109.12.122的IP2。我该怎么做了。

I need to store 10.172.21.121 in ip1 & 10.109.12.122 in ip2. How do i do that

感谢

推荐答案

如果您已经使用升压,为什么不IP地址,适当的类的对象存储?

If you are already using boost, why not store IP addresses in objects of appropriate class?

#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';
}

如果您将其转换为整数,你可以这样做必要的时候,用<一个href=\"http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/reference/ip__address_v4/to_bytes.html\">to_bytes例如。

If you have to convert them to integers, you can do so when necessary, with to_bytes for example.

这篇关于使用boost分割字符串::算法::分裂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆