Advertisement
AntonioVillanueva

Uso de la libreria boost separar una ip

Sep 8th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. // separa una IP utilizando la libreria boost
  2. //sudo apt-get install libboost-all-dev
  3. #include <iostream>
  4. #include <boost/tokenizer.hpp>
  5. #include <string>
  6. typedef struct {unsigned short p[4];}st_ip;
  7.  
  8. int main()
  9. {
  10.     size_t pos(0);
  11.     st_ip ip;
  12.   std::string str = "192 168.6.189";
  13.   typedef boost::tokenizer<boost::char_separator<char> >  tokenizer;
  14.   boost::char_separator<char> sep("-. ");
  15.   tokenizer tokens(str, sep);
  16.  
  17.   for (tokenizer::iterator tok_iter = tokens.begin();tok_iter != tokens.end(); ++tok_iter)
  18.    {
  19.     std::cout << *tok_iter << ".";
  20.    
  21.    // unsigned short k=stoi( *tok_iter);
  22.     ip.p[pos++]=stoi( *tok_iter);
  23.     }
  24.    
  25.     std::cout<<std::endl;
  26.     //Test struct
  27.     for (size_t i=0;i<sizeof(ip.p)/sizeof(*ip.p);i++){
  28.         std::cout <<ip.p[i]<<std::endl;
  29.     }
  30.    
  31.    
  32.   std::cout << "\n";
  33.   return EXIT_SUCCESS;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement