读取并解析 C/C++; 中的行将标记放入数组或向量或类似结构中
我必须针对 ACM IPC 中的一个问题提交代码,您可能知道,时间非常重要。所以,我必须有效地读取这样的输入: 第一行将包含关联的整数值序列,第二行将包含与另一个序列关联的整数值序列。 例如:
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
我必须将第一行放在一个数组中,将第二行放在另一个数组中,并将两者传递到一个函数中。
我如何读取这些内容并将其放入 C/C++ 中?我正在以 C 方式思考,但我的方法将有 2 while 的... 我偏好使用 scanf 进行读取,但解析可以按照您的意愿进行。
请帮助这个新手!
I have to submit code to one of the problems in ACM IPC and, as you may know, the time counts a lot. So, I have to read efficiently an input like this:
The first line will contain the sequence of integer values associated and the second line will contain the sequence of integer values associated with another sequence.
E.g.:
3 2 1 4 5 7 6
3 1 2 5 6 7 4
7 8 11 3 5 16 12 18
8 3 11 7 16 18 12 5
255
255
I have to put the 1st line in an array and the second in another and pass both in a function.
How do I read and put these in C/C++? I was thinking in a C way, but my approach would have 2 while's...
I have preference reading with scanf, but the parsing can be done as you want.
Please , help this newb!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
std::getline()
< 读取这些行/a>.然后使用std::stringstream
来解析每行。由于这是一场竞赛,因此您不需要实际的代码。Read the lines using
std::getline()
. Then use astd::stringstream
to parse each line. As this is for a competition, you won't be wanting actual code.您还可以使用 strtok() 和 strdup()。
请参阅使用 strtok() 的示例和strdup()。然后将使用 strtok() 提取各个标记 - strdup() 分配空间并复制它们。
You could also use strtok() and strdup().
See example of using strtok() and strdup(). strtok() would then be used to extract the individual tokens - strdup() to allocate space an copy them.