C++:如何使用 STL 逐行迭代 std::string 中的文本?

发布于 2024-10-02 13:03:57 字数 329 浏览 0 评论 0原文

我在 std::string 对象中有一个文本。文本由几行组成。我想使用 STL(或 Boost)逐行迭代文本。我想出的所有解决方案似乎都远非优雅。我最好的方法是在换行符处分割文本。有更优雅的解决方案吗?

更新:这就是我一直在寻找的:

std::string input;
// get input ...
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
  std::cout << line << std::endl;
}

I have a text in a std::string object. The text consists of several lines. I want to iterate over the text line by line using STL (or Boost). All solutions I come up with seem to be far from elegant. My best approach is to split the text at the line breaks. Is there a more elegant solution?

UPDATE: This is what I was looking for:

std::string input;
// get input ...
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line)) {
  std::cout << line << std::endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-10-09 13:03:57

为什么将文本保留在源文件中?将其保存在单独的文本文件中。使用 std::ifstream 打开它并使用 while(getline(...)) 迭代它。

#include <iostream>
#include <fstream>

int main()
{
   std::ifstream  fin("MyText.txt");
   std::string    file_line;
   while(std::getline(fin, file_line))
   {
      //current line of text is in file_line, not including the \n 
   }
}

或者,如果文本必须位于 std::string 变量中以类似的方式使用 std::istringstream 逐行读取

如果您的问题是如何在不使用 + 的情况下按词法将文本放入代码中,请注意,相邻的字符串文字在编译之前已连接,因此您可以这样做:

std::string text = 
   "Line 1 contents\n"
   "Line 2 contents\n"
   "Line 3 contents\n";

Why do you keep the text in your source file? Keep it in a separate text file. Open it with std::ifstream and iterate over it with while(getline(...))

#include <iostream>
#include <fstream>

int main()
{
   std::ifstream  fin("MyText.txt");
   std::string    file_line;
   while(std::getline(fin, file_line))
   {
      //current line of text is in file_line, not including the \n 
   }
}

Alternatively, if the text HAS to be in a std::string variable read line by line using std::istringstream in a similar manner

If your question is how to put the text lexially into your code without using +, please note that adjacent string literals are concatenated before compilation, so you could do this:

std::string text = 
   "Line 1 contents\n"
   "Line 2 contents\n"
   "Line 3 contents\n";
丶情人眼里出诗心の 2024-10-09 13:03:57

使用 Boost.Tokenizer:

std::string text("foo\n\nbar\nbaz");

typedef boost::tokenizer<boost::char_separator<char> > line_tokenizer;
line_tokenizer tok(text, boost::char_separator<char>("\n\r"));

for (line_tokenizer::const_iterator i = tok.begin(), end = tok.end();
     i != end ; ++i)
    std::cout << *i << std::endl;

prints

foo
bar
baz

请注意,它会跳过空行,这可能会导致或者可能不是你想要的。

Use Boost.Tokenizer:

std::string text("foo\n\nbar\nbaz");

typedef boost::tokenizer<boost::char_separator<char> > line_tokenizer;
line_tokenizer tok(text, boost::char_separator<char>("\n\r"));

for (line_tokenizer::const_iterator i = tok.begin(), end = tok.end();
     i != end ; ++i)
    std::cout << *i << std::endl;

prints

foo
bar
baz

Note that it skips over empty lines, which may or may not be what you want.

半城柳色半声笛 2024-10-09 13:03:57

如果您想逐行循环,正如您所说,为什么在换行符处分割文本不正是您想要的?

您没有发布代码来显示您是如何做到这一点的,但您的方法似乎正确地完成了您所说的目标。为什么会感觉低人一等?

If you want to loop line by line, as you say, why would splitting the text at line breaks not be exactly what you want?

You didn't post code showing how you're doing it, but your approach seems correct to accomplish what you said you wanted. Why does it feel inferior?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文