STL String :: length()SEGFAULTing

Dav*_*mes 5 c++ string stl string-length segmentation-fault

#include <iostream>
#include <string>
#include <vector>

/*
  Using STL's string class because the problem does not refer any
  limits regarding the number of characters per line.
 */

using namespace std;

int main()
{
  string line;
  vector<string> lines;
  while (getline(cin, line))
  {
    lines.push_back(line);
  }

  unsigned int i, u;
  unsigned int opening = 1; // 2 if last was opening, 1 if it was closing
  for (i = 0; i < (int) lines.size(); i++)
  {
    for (u = 0; u < (int) lines[u].length(); u++)
    {

    }
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我有那个简单的代码,只读取几行(输入文件):

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"
Run Code Online (Sandbox Code Playgroud)

但是,我发现它是SEGFAULTing,因为它在第一行(第4个字符)上读取''(空格)字符:

(gdb) run < texquotes_input.txt 
Starting program: /home/david/src/oni/texquotes < texquotes_input.txt

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b92533 in std::string::length() const () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么,我在循环中没有做任何事情,我只是循环.

Dav*_*mes 6

我已经发现了这个问题.这是内循环:

for (u = 0; u < (int) lines[u].length(); u++)
{

}
Run Code Online (Sandbox Code Playgroud)

应该:

for (u = 0; u < (int) lines[i].length(); u++)
{

}
Run Code Online (Sandbox Code Playgroud)