C++ 字符串到 int 值

标签 c++ string time

在问这个问题之前我一直在到处寻找,但没有找到任何东西。 在我自学的 Schaums Programming with C++ Book 中没有提到它...

如何使用 C++ 将字符串(例如 "0:03:22")转换为 3 个独立的 int 值0、0322`?假设它可能。

最佳答案

有点像

std::string str="0:03:22";
std::istringstream ss(str);
int hours,mins,seconds;
char skip;
if(ss >> hours >> skip >> mins >> skip >> seconds) {
    //success
}

这里我们创建了一个流,我们可以从中提取每个元素。

引用

http://en.cppreference.com/w/cpp/io/basic_stringstream

关于C++ 字符串到 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13782053/

相关文章:

c++ - C++中的对象销毁

Java - 匹配字符串中的特定URL

javascript - Highcharts 中样条图上的重叠数据

python - 测量 `UnboundLocalError`的时间时出现 `torch.where`错误

c++ - 用户定义的限定符

c++ - std::array 求最大值函数

python - 在Content disposition response file name-python/django中添加一个变量

r - 时间数据的ggplot2和chron条形图scale_x_chron

C++ 链表 : IndexOf of an object

python - 为什么这个字符串比较返回 False?