【问题标题】:parsing informations from an ostringstream从 ostringstream 解析信息
【发布时间】:2015-01-04 14:16:30
【问题描述】:

我有这个 ostringstream,其中包含有关文件夹和文件的信息,文件夹列表及其信息从“文件夹:”开始并在“文件:”之前结束,而文件列表从包含“文件:”的行开始" 直到流结束,我的目标是从该 ostringstream 生成两个向量,一个用于文件夹,另一个用于文件,在这些向量中,我想为每个文件夹或文件使用映射来存储与它们相关的信息,

示例:包含 3 个 map<string,string> 类型项目的向量“MyFolders”,在此地图内 MyFolders[0]["id"] 将返回例如 "109" MyFiles[0]["name"] 将返回例如 "a_file.jpg"

要解析的ostringstream的内容:

folders:
:
id: 109
parent_id: 110
name: first_folder
// other informations about a folder ...
path: /1/105/110/
:
id: 110
parent_id: 103
name: second_folder
// other informations about a file ...
path: /1/105/
files:
:
id: 26
parent_id: 105
name: a_file.jpg
size: 7345
// other informations about a file ...
path: /1/105/
:
id: 69
parent_id: 105
name: another_file.txt
// other informations about a file ...
size: 7345
path: /1/105/

【问题讨论】:

  • 你在哪里遇到问题?你有没有尝试过?
  • 我不知道有什么函数/工具可以帮助我从行中提取信息,否则我将能够想到算法然后编写代码
  • 如果你想读一行你可以使用std::getline
  • 谢谢,如果我想从像“id: 105”、“id”和“105”(没有空格“105”)这样的字符串中提取,你能告诉我什么?我想做这样的事情 MyFolder[0][string_that_contains_id] = string_that_contains_105

标签: c++ string parsing dictionary vector


【解决方案1】:

您可以使用 2 个向量,例如

vector<int,int,char*,char*>file and //for saving id,parent id,name and path
vector<int,int,char*,char*>folder

并使用istream::getline检查此行是否为"file" or "folder"

要了解有关 getline 和文件功能的更多信息,您可以从我在评论部分提供的链接中查看实现和逻辑。

【讨论】:

  • 谢谢,但我更喜欢使用字符串作为 id、p_id 等...因为我在 HTTP 请求中使用它们(更容易被视为字符串),否则,我知道使用一种类型的向量,像 vector 例子;我不知道那种具有多种类型的向量
  • 好的我明白你说的了,我试试看
猜你喜欢
  • 1970-01-01
  • 2011-07-14
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
相关资源
最近更新 更多