C++ 如何从句子中读取和存储值
c++ how to read and store values from a sentence
所以有一个输入文件包含类似的东西
名称1 到达 20 服务 20 名称2 到达 22 服务 11 name3 到达 23 服务 40
我正在尝试在字符串名称中存储名称 1、名称 2、名称 3, 到达 (20, 22, 23( 在 int 到达,服务 (20, 11 ,40( 在 int 服务。 我知道我将不得不使用 while 循环和获取线,但我真的不确定该怎么做。谢谢
为您整理了一个快速示例程序。基本上,您需要使用字符串流来存储到所需的任何数据结构中。根据您使用的架构,可能会有特殊的解决方案,但希望这能帮助您入门。
std::string str = "name1 arrival 20 service 20nname2 arrival 22 service 11nname3 arrival 23 service 40";
std::stringstream iss(str);
// Create temporary variable to store each line
std::string line;
// Data to hold each item
std::string name[3];
int arrival[3];
int service[3];
std::string trash; // For throwing out data
// Read stream line by line
int index = 0;
while (getline(iss, line))
{
// Parse the line
std::stringstream lineStream(line);
// Read each attribute into the appropriate data structure
lineStream >> name[index];
lineStream >> trash;
lineStream >> arrival[index];
lineStream >> trash;
lineStream >> service[index];
// Increment index
++index;
}
// Print out the results
for (int i = 0; i < 3; ++i)
{
std::cout << "Index[" << i << "]: " << name[i]
<< ", " << arrival[i] << ", " << service[i]
<< std::endl;
}
相关文章:
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 将从格式化文本文件读取的文本数据存储到链表
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 读取文件并将其存储在unordered_map中时出现问题
- 如何读取 TXT 文件并仅将 int 存储在 C 中
- 读取和存储 CSV 数据
- 读取文件并将其内容存储到 2-D 数组C++中
- 从文本文件读取,然后将项目存储到列表中
- 如何读取文件并将该文件分类为不同类型的数据,这些数据都存储在结构中的特定数组中
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 如何从excel文件中读取数据并存储到变量中?
- 如何在不知道大小的情况下读取文本文件并存储到数组中
- 如何在C++中读取并立即丢弃一些数据,而不存储这些数据
- C++-需要读取存储在文件(.txt)中的时间戳并正确检索
- 从文件中读取后,将分号分隔的整数存储到数组中
- 是防止原子存储部分读取所必需的"memory_order_relaxed"
- 如何读取存储在输入流缓冲区中的字符数
- 我正在尝试通过 Boost 图形库从 Graphviz DOT 文件中读取图形.如何读取存储在数组中的未知数量的属性
- 如何读取存储在HDF5中的ArrayType的一个组件
- 读取存储在双精度体中的8字节字段中的4个字节