gpt4 book ai didi

c++ - 使用 cin 附加到字符串 C++

转载 作者:行者123 更新时间:2023-12-03 07:04:34 25 4
gpt4 key购买 nike

有没有办法使用 std::cin 附加到字符串而不是替换里面的内容?

或者有更好的选择吗?

最佳答案

你可以尝试这样的事情:

#include <iostream>
#include <iterator>
#include <string>

int main()
{
std::istream_iterator<std::string> it(std::cin);
std::istream_iterator<std::string> end_it;
std::string str;
for (int i = 0; i < 3; ++i)
if (it != end_it) {
str += *it;
if (i != 2)
++it;
} else {
break;
}
std::cout << "Res: " << str << "\n";
}

但使用两个 std::string 变量要简单得多,一个用于电流输入,另一个用于累积。

关于c++ - 使用 cin 附加到字符串 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704144/

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