我想将以HH:MM:SS格式化的字符串时间戳转换为仅秒,然后将其与数字进行比较。我用Java编写了我的代码的主要版本,但是我单独向Scanner询问,而不是string
时间。我对C ++库不是很熟悉,因为我是一个Java人。想知道我怎么能用C ++做到这一点?
简明扼要,String s = "1:01:01";
和String s2= "3600";
我需要了解if (s>s2)
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
int hours;
int mins;
int secs;
System.out.println("Enter Hours: ");
hours = console.nextInt();
System.out.println("Enter Minutes: ");
mins = console.nextInt();
System.out.println("Enter Seconds: ");
secs = console.nextInt();
int showSecs = (hours * 3600) + (mins * 60) + secs;
System.out.println(hours + ":" + mins + ":" + secs + " in secs are "
+ showSecs);
}
}
答案 0 :(得分:8)
我会冒险投票,并提醒您我们的工具箱中仍有sscanf
。
int h, m, s= 0;
std::string time ="10:40:03"
if (sscanf(time.c_str(), "%d:%d:%d", &h, &m, &s) >= 2)
{
int secs = h *3600 + m*60 + s;
}
答案 1 :(得分:4)
正如@ghostofstandardspast建议您可以使用std::get_time()
I / O操纵器从std::istream
#include <iostream>
#include <sstream>
#include <locale>
#include <iomanip>
#include <ctime>
int main() {
std::tm t;
std::istringstream ss("1:01:01");
ss >> std::get_time(&t, "%H:%M:%S");
std::cout << "Total seconds: "
<< t.tm_hour * 3600 + t.tm_min * 60 + t.tm_sec
<< std::endl;
}
这是 fully working sample 使用clang。不幸的是,我无法使用GCC 4.8.x运行此示例,我猜它在此实现中并未完成。可能是GCC 4.9.x正确支持。
在寻找替代方案时(如果您不能使用实际支持完整的当前c++11标准的编译器),您可以考虑将std::sscanf()
用作@Roddy suggested,或split the string使用':'
作为分隔符,并简单地将拆分部分转换为整数值,使用例如atoi()
方法
Here's the alternative
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> &split
( const std::string &s
, char delim
, std::vector<std::string> &elems)
{
std::istringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
int main() {
std::vector<std::string> parts;
split("1:01:01",':',parts);
if(parts.size() == 3) {
int hour = std::atoi(parts[0].c_str());
int min = std::atoi(parts[1].c_str());
int sec = std::atoi(parts[2].c_str());
std::cout << "Total seconds: "
<< hour * 3600 + min * 60 + sec
<< std::endl;
}
return 0;
}
答案 2 :(得分:1)
让我们做一个解析字符串的简单自动机:
#include <string>
int string2sec(const std::string& str) {
int i = 0;
int res = -1;
int tmp = 0;
int state = 0;
while(str[i] != '\0') {
// If we got a digit
if(str[i] >= '0' && str[i] <= '9') {
tmp = tmp * 10 + (str[i] - '0');
}
// Or if we got a colon
else if(str[i] == ':') {
// If we were reading the hours
if(state == 0) {
res = 3600 * tmp;
}
// Or if we were reading the minutes
else if(state == 1) {
if(tmp > 60) {
return -1;
}
res += 60 * tmp;
}
// Or we got an extra colon
else {
return -1;
}
state++;
tmp = 0;
}
// Or we got something wrong
else {
return -1;
}
i++;
}
// If we were reading the seconds when we reached the end
if(state == 2 && tmp < 60) {
return res + tmp;
}
// Or if we were not, something is wrong in the given string
else {
return -1;
}
}
尚未经过测试,因此没有保修。
编辑:我仍然无法承诺,但我进行了快速测试,似乎按预期正常工作。#include <iostream>
int main(int argc, char** argv) {
(void) argc;
(void) argv;
std::cout << string2sec("0:2:0") << std::endl;
std::cout << string2sec("1:0:0") << std::endl;
std::cout << string2sec("1:2:0") << std::endl;
std::cout << string2sec("10:10:10") << std::endl;
return 0;
}
输出:
120
3600
3720
36610