简体   繁体   English

字符串向量不能与换行符和迭代器一起使用? (C ++)

[英]String vectors not working as expected with newline and iterators? (C++)

I have a text file made of 3 lines: 我有一个由3行组成的文本文件:

Line 1

Line 3

(Line 1, a blank line, and Line 3) (第1行,空白行和第3行)

vector<string> text;
vector<string>::iterator it;
ifstream file("test.txt");
string str;

while (getline(file, str))
{
  if (str.length() == 0)
    str = "\n";
  // since getline discards the newline character, replacing blank strings with newline

  text.push_back(str);
} // while

for (it=text.begin(); it < text.end(); it++) 
  cout << (*it);

Prints out: 打印输出:

Line 1
Line 3

I'm not sure why the string with only a newline was not printed out. 我不确定为什么只有换行符的字符串没有打印出来。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Wasn't? 不是吗 Actually, it was! 其实是! The reason you have a newline after Line 1 is exactly that empty string with newline in it and nothing else. Line 1行之后有换行符的原因恰好是其中包含换行符的空字符串,仅此而已。 If not for that second line, you'd see Line 1Line 3 as output. 如果不是第二行,您将看到Line 1Line 3作为输出。 (You said it yourself: getline discards newline characters.) (您自己说过: getline放弃换行符。)

Apparently, the way I understand your intent, you were supposed to implement your output cycle as follows 显然,据我了解您的意图的方式,您应该按照以下方式实现输出周期

for (it = text.begin(); it < text.end(); it++)  
  cout << *it << endl; 

That way you add a newline after each string during output. 这样,您可以在输出期间在每个字符串之后添加换行符。 But if so, then you don't need to manually add a \\n character to empty strings during reading. 但是,如果是这样,则在读取过程中无需在空字符串中手动添加\\n字符。

In other words, decide what is it you want to do. 换句话说,决定要做什么。 At this time it is not clear. 目前尚不清楚。

  • If you want to restore the discarded newline characters during reading, you have to do it for all lines, not just for empty ones. 如果要在读取过程中恢复丢弃的换行符,则必须对所有行都执行此操作,而不仅仅是对空行。

  • If you want to add the newline characters during output, you don't need to explictly push them into the read lines at all. 如果要在输出过程中添加换行符,则根本不需要将它们明确地推入读取行中。

In fact, it is a rather strange idea to literally push the newline characters into your strings. 实际上,将换行符逐字地推入字符串中是一个很奇怪的想法。 What for? 做什么的? Since you already read and store your text line-by-line, the newline characters can be implied . 由于您已经逐行阅读和存储了文本,因此可以隐含换行符。 Ie you can do the printing as I do it above (with endl ), and everything will look as expected. 也就是说,您可以像上面一样(使用endl )进行打印,并且一切都会按预期进行。

I think the simple answer here, is that getline() strips the trailing newline whether or not there is content in the string. 我认为这里的简单答案是,无论字符串中是否包含内容,getline()都会删除尾随的换行符。 So the three reads you do are as follows: 因此,您执行的三读操作如下:

"Line 1"
""
"Line 3"

which you transform into: 您将其转换为:

"Line 1"
"\n"
"Line 3"

which when printed is: 打印时为:

Line 1
Line 3

I'd use something like this: 我会用这样的东西:

std::vector<std::string> text;  
std::string str;

while (std::getline(infile, str))
    text.push_back(str);

std::copy(text.begin(), text.end(), 
    std::ostream_iterator<std::string>(std::cout, "\n"));

You're adding complexity that stops your code from working. 您添加的复杂性使您的代码无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM