gpt4 book ai didi

c++ - 如何快速提取这三个数字?

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:30 27 4
gpt4 key购买 nike

我有一个 std::string ,其中包含 3 个由空格分隔的数字例如:123334 33335 54544。如何快速提取这三个数字?

int r = ... /* 123334 */
int g = ... /* 33335 */
int b = ... /* 54544*/

最佳答案

最简单的方法是使用 stringstream :

std::string numbers = "123334 33335 54544";
std::istringstream parse(numbers);
int r, g, b;

parse >> r;
parse >> g;
parse >> b;

if (!parse)
throw std::runtime_error("invalid string");

关于c++ - 如何快速提取这三个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5489184/

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