在c ++中打破字符串

时间:2017-12-01 15:41:33

标签: c++

我试图在不同的块中打破字符串并将这些块存储在字符串数组中以供进一步实现,但我遇到了一些问题。 例如,当我输入172.16.524.1时,它将其存储为172.16.524.0 这是我的代码:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
   string address;
   string str1[4];
   int found,i=0,n1[4],n2,n3,n4;
   cin>>address;
   for(int a=0 ; a<address.length() ; a++)
   {
       found=address.find(".");
       for(int f=0 ; f<found ; f++)
       {
           str1[i]+=address[f];
       }
       i++;
       address.erase(0,found+1);
   }
   for(int j=0 ; j<i ; j++)
   {
       n1[j]=atoi(str1[j].c_str()); //To convert String int to integer
       cout<<n1[j]<<" ";
   }
   cout<<endl;
   return 0;
}

3 个答案:

答案 0 :(得分:0)

您正在将旧式C字符串与新式C ++字符串混合使用

   found=address.find(".");
   str1[i] = address.substr(0, found) ;
   if (found != std::string::npos) // dot found
        address.erase(0,found+1);
   else
        address.clear() ; 
   i++ ;

应该做你想要的,但是使用std :: vector可能会有所帮助,所以下面的内容有点整洁

   std::vector<std::string> str1 ;
   ... 
   found=address.find(".");
   str1.push_back(address.substr(0, found)) ;
   if (found != std::string::npos) // dot found
        address.erase(0,found+1);
   else
        address.clear() ; 

答案 1 :(得分:0)

std::string s = "172.168.1.15";
std::string delimiter = ".";
size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos)
{
token = s.substr(0, pos);
std::cout << token << std::endl;
s.erase(0, pos + delimiter.length());
}
std::cout << s << std::endl;

解析字符串see this question

的标准方法

答案 2 :(得分:0)

  

我有一些问题

我更喜欢std :: stringstream。 std :: getline()的第3个参数是一个分隔符值,似乎很适合这个挑战。

在下面的代码中,我将每个字段提取为字符串,并将其推送到字符串向量(addrFields)。

要报告结果,我列出了字段,然后再次输出,并将字段转换为带有std :: stoi()的整数。

注意:我追加'。'对地址进行分隔...这可以避免eof()截断最后一个字段。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cassert>

int exec()
{
   std::string address ("172.16.524.1");
   std::cout << "\n\n  echo of input:     '" << address << "'" << std::endl;

   std::vector<std::string> addrFields; 
   {
      // fill ss with the address    vvv  append one delim
      std::stringstream ss(address + '.');
      std::cout << "  ss contents:       '" << ss.str() 
                << "'" << std::endl;

      do // loop through fields
      {
         std::string f;
         (void)std::getline(ss, f, '.'); // each field ends at delim

         if (false == ss.good())
         {
            if (ss.eof()) break;  // quietly exit, a normal op

            // something wrong with input
            std::cerr << "\n  err Line: " << address << std::endl;

            assert(0); // no use continuing with bad input
         }

         if(f.size()) // ignore blank fields
            addrFields.push_back(f);

         // consider using addrFields.size() 
         //    for special handling 4 or 6 fields
         //    for special handling of a colon appended port num

      } while(true);
   }

   std::cout << "\n  Results  " << std::endl;
   std::cout << "\n      addrFields :   ";

   for (auto f : addrFields)
      std::cout << f << "   ";

   std::cout << "  (strings)   "
             << "\n\n       addr ints :   ";

   for (auto f : addrFields)
      std::cout << std::stoi(f) << "   ";

   std::cout << "  (converted to ints) " << std::endl;

   return 0;
}

输出是:

  echo of input:     '172.16.524.1'
  ss contents:       '172.16.524.1.'

  Results  

      addrFields :   172   16   524   1     (strings)   

       addr ints :   172   16   524   1     (converted to ints)