列表 - 字符串 - 文本文件

发布于 2024-08-30 09:58:29 字数 144 浏览 1 评论 0原文

我有一些关于文本文件、列表和字符串的问题。

我想知道是否可以放入读取文本文件中文本的代码,然后使用“string line;”或其他东西来定义文本的每个新行并将它们全部变成一个列表。因此,我可以对行进行排序,删除一行或两行甚至全部行,或者在文本中搜索特定行。

I've got a few questions concerning text files,list and strings.

I wonder if it is possible to put in a code which reads the text in a textfile,and then using "string line;" or something else to define each new row of the text and turn all of them into one list. So I can sort the rows, remove a row or two or even all of them or search through the text for a specific row.

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

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

发布评论

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

评论(2

慈悲佛祖 2024-09-06 09:58:30

在 C++ 中,您通常使用 std::vector 来执行此操作:

std::vector<std::string> data;

std::string temp;

while (std::getline(infile, temp))
    data.push_back(temp);

对它们进行排序将如下所示:

std::sort(data.begin(), data.end());

删除第 N 行将如下所示:

data.erase(data.begin() + N);

In C++, you'd typically do this with an std::vector:

std::vector<std::string> data;

std::string temp;

while (std::getline(infile, temp))
    data.push_back(temp);

Sorting them would then look like:

std::sort(data.begin(), data.end());

Deleting row N would look like:

data.erase(data.begin() + N);
不美如何 2024-09-06 09:58:30

未指定语言,但我想它背后的设计是相同的:

  • 读入数据并将每段文本存储在某个“字符串”结构中将
  • 每段数据存储在列表类型对象中(例如,std: :c++ 中的向量)
  • 定义或利用某些接口来执行请求的操作,

我想这只是哪种语言的问题。所以是的,这是可能的。

The language wasn't specified but I would imagine the design behind it would be the same:

  • Read in your data and store each piece of text in some 'string' structure
  • Store each piece of data in a List type object (eg, std::vector in c++)
  • Define or utilize some interface to perform the requested operations

I suppose then it will be just a matter of which language. So yes it is possible.

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