从空格的字符串获取int的最佳方式? [英] Best way to get ints from a string with whitespace?
本文介绍了从空格的字符串获取int的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道这很简单,我只是不记得最好的方法做到这一点。
我有一个输入,例如定义2D向量数组的x和y的5 15
。
我只需要这两个数字到 int col
和 int row
。
I know this is simple, I just can't recall the best way to do this.
I have an input like " 5 15 "
that defines the x and y of a 2D vector array.
I simply need those two numbers into int col
and int row
.
这是最好的方法是什么?我正在调用stringstreams,但无法找出正确的代码。
What's the best way to do this? I was attemping stringstreams, but can't figure out the correct code.
感谢任何帮助!
推荐答案
您可以使用 stringstream
:
std::string s = " 5 15 ";
std::stringstream ss(s);
int row, column;
ss >> row >> column;
if (!ss)
{
// Do error handling because the extraction failed
}
这篇关于从空格的字符串获取int的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文