Jagoda
Jagoda

Reputation: 424

Substring function in vector

How can I use substr() on objects in vector of strings?

vector<string>text[7];
for (int j=0;j<7;j++)
{
   for (int y=0;y <text[i].size(); y++)
   {
       text[j][y].substr(1,1);
   }
}

Upvotes: 0

Views: 1359

Answers (2)

herohuyongtao
herohuyongtao

Reputation: 50657

  1. Change

    for (int y=0;y <text[i].size(); i++)
    

    to

    for (int y=0; y<text[j].size(); j++) // as no "i" here
    
  2. substr() will return a string, so you probably need to use a string to receive its return value (although can still compile successfully if not), otherwise, it's meaningless to do substr() here.

    string str = text[j][y].substr(1,1);
    

Upvotes: 2

Raul Andres
Raul Andres

Reputation: 3806

   for (int y=0;y <text[i].size(); i++)

Try increasing y instead of i

   for (int y=0;y <text[i].size(); y++)

Normally, people uses i,j,k,l for nested loops. But if you like y, you should increase it.

Upvotes: 1

Related Questions