gpt4 book ai didi

C++多行输入

转载 作者:行者123 更新时间:2023-11-28 03:54:23 25 4
gpt4 key购买 nike

所以我需要创建一个包含 4 行文本 (EN_us) 的字符缓冲区

first line
line with some number like 5
line 3
empty line

从用户那里获取这种字符缓冲区的正确方法是什么以及如何获取它的长度?

最佳答案

与其获取这样的缓冲区,不如使用 getline 从标准输入中将四行读入单独的 string 可能更容易(如果您愿意,可以使用循环):

然后总数据长度是各个string 长度的总和。或者使用此方法从用户那里检索数据,然后将它们连接成四行 stringstream

组合代码示例:

#include <string>
#include <sstream>
#include <iostream>

std::string s[4];
size_t length(0);

std::ostringstream output;

for (size_t index = 0; index < 4; ++index)
{
getline(std::cin, s[index]);
length += s[index].length();

output << s[index] << std::endl;
}

output.flush();
streamoff streamLength = output.tellp();

关于C++多行输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com