Permuted index question from accelerated c++

Hello, in the book accelerated c++ , chapter five-question 1-
we are asked to write a program that get input of a few sentences and
write the output according to a permuted index.

Although I tried and inquired for hours , I wasn't being able to come up with a working solution.
Lastly I searched and found a working solution that someone posted on github.

Don't know if I can post the whole code here, but here is the link:
https://github.com/bitsai/book-exercises/blob/master/Accelerated%20C%2B%2B/chapter05/5-1.cpp
(together with split.cc in the same folder)

What I don't understand about this solution is why the 'first' member in the struct rot in the function rotate_line,for the first iteration-should start from 4?
I mean for the input 'the quick brown fox' ,the size of words is 4,but that is not the index of the first word in the sentence?

1
2
3
4
5
 struct Rotation {
  vector<string>::size_type first;
  vector<string> words;
};
}



1
2
3
4
5
6
7
8
9
10
vector<Rotation> rotate_line(string line) {
  vector<Rotation> rotations;
vector<string> words = split(line);
 for (vector<string>::size_type i = 0; i < words.size(); ++i) {
Rotation rot = {words.size() - i, words};
  rotations.push_back(rot);
    rotate(words.begin(), words.begin() + 1, words.end());
}
 return rotations;
}




After sorting the rotations, the structures look like this:
rotations
{
{0{first=2, words{brown,fox,the, quick}
{1{first=1, words{fox,the, quick,brown}
{2{first=3, words{quick,brown,fox, the}
{3{first=4, words{the, quick,brown,fox}

}

Later in the print_rotations function, we use the 'first' member to find the separator, of before and after the first word in the rotated sentence-to my understanding- and I don't understand the use of the size '4' for the 'first' member of rot struct, for the sentence 'the quick brown fox' iteration ?I mean,it seems to work when assigning to 'first' member the size of words (which is 4)in the rotate_line function, but that is not the index of the first word?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 void print_rotations(vector<Rotation> rotations) {
  vector<string> first_halves;
  vector<string> second_halves;
  string::size_type max_first_half_width = 0;

  for (vector<Rotation>::size_type i = 0; i < rotations.size(); ++i) {
    Rotation rot = rotations[i];
    string first_half, second_half;

    for (vector<string>::size_type j = rot.first; j < rot.words.size(); ++j)
      first_half += rot.words[j] + " ";

    first_halves.push_back(first_half);

    if (first_half.size() > max_first_half_width)
      max_first_half_width = first_half.size();

    for (vector<string>::size_type j = 0; j < rot.first; ++j)
      second_half += rot.words[j] + " ";

    second_halves.push_back(second_half);
  }

  for (vector<string>::size_type i = 0; i < first_halves.size(); ++i) {
    cout << setw(max_first_half_width);
    cout << first_halves[i];
    cout << "\t";
    cout << second_halves[i];
    cout << endl;
  }
}


Hope my question is clear enough as I try to understand the solution better,I use this code in vs2015 and the output seems to work right.

I would appreciate any further insight to this ,thanks.
Topic archived. No new replies allowed.