-3

I have this string which contains numbers divided from one another with a /, like this 24/2/13. I want to save them individually in a vector of int,but it gives me this mistake expected unqualified-id before '.' token|. I know it might be a stupid mistake but that's just the qay for now :). Here is the code:


int t;
string s;
for(int i=0;i<final_states.size();i++)
{
    if(final_states.at(i)!='/')
        s.push_back(final_states.at(i));
    else
    {
        t=atoi(s.c_str());
        temp_final_states.push_back(t);
        s.clear();
    }
}

3 Answers3

0

I assume this is a compile error. It looks like you are iterating over final_states. But then you try to call push_back() on temp_final_states. I assume you intend this to be the same collection.

geipel
  • 395
  • 1
  • 3
  • 9
0

Refer following sample code http://ideone.com/id7E22

char str[] ="22/33/44";
  char * pch;
  cout<<"Splitting string into tokens:\n"<<str;
  pch = strtok (str,"/");
vector<int> vec;
  while (pch != NULL)
  {
    int val = atoi(pch);
    cout<<"   "<<val;
    vec.push_back(val);
    pch = strtok (NULL, "/");
  }
shivakumar
  • 3,297
  • 19
  • 28
0

Assuming your string always starts with a numeric value and the '/' character is not at the end of the string this should do it for you:

std::string final_states="24/2/13";
std::vector<int> temp_final_states;

temp_final_states.push_back(atoi(final_states.c_str()));
std::string::size_type index = final_states.find('/');

for (; index != std::string::npos; index = final_states.find('/', index) )
{
    ++index;
    const char* str = final_states.c_str() + index;
    temp_final_states.push_back(atoi(str));
}