0

我以前从未使用过 istringstream。我之前只使用 1 个分隔符分割行,所以我不知道如何使用 istringstream。我正在从一个看起来像这样的文件中拆分行:

表,通缉,100

汽车, 出售, 5000

我需要拆分字符串,然后创建一个结构数组。我已经设置了一个结构我只是不知道如何拆分字符串。我的结构称为 item 并且具有类型:字符串类型、布尔销售、双倍价格。出售,我想让它说 1 如果它是出售的,0 如果它是想要的。基本上,我想拆分它,以便创建新变量、类型、销售和价格,然后在我的项目结构中创建一个 newItem{type, sale, price} 并从那里开始。代码示例将非常有帮助。非常感谢。

4

1 回答 1

1

使用分隔符拆分字符串的一种简单方法是使用std::getline.

std::string line = "Table, Wanted, 100"; // Let's say you have read a line from file.
std::istringstream input{line};          // Create an input stream from string.

// Read all characters up until the delimiter ',' on each iteration.
for (std::string token; std::getline(input, token, ',');) {
    /* Do something with each token... */
}

Live example

于 2015-01-20T01:09:26.970 回答