C++ 在字符串中分解时间的条件
C++ Condition to break apart time in string
我的代码有一个问题,涉及如何获取表示时间的字符串数据成员,由它包含的冒号数处理,然后分隔并转换为整数。
目标: 取一串时间,以 ##:##:##:## 的形式表示,用 1 到 3 个冒号分隔,例如 1:13:0:59,并"切碎"冒号之间的各个片段。
可以省略零的前导日和小时字段,因此 0:0:0:12、0:0:12 和 0:12 都是可接受的"12 秒"输入形式。"12"是不可接受的。但是,值可能会超过秒、分钟和小时的常规限制。例如,25:3:90 和 1:1:4:30 。
当我输入 0:01 时,分钟会得到整个值。我不确定还有另一种方法可以分解字符串并从中创建单独的整数。
这是我到目前为止的代码。
struct Times {
int days;
int hours;
int minutes;
int seconds;
string time;
void string_to_ints(string& s);
}
void string_to_ints(string& s) {
// count the amount of colons
for (int i = 0; i < time.length(); i++) {
if (time[i] == ':')
count++;
}
// initialize days hours minutes and seconds
if (count == 3) {
day = time.substr(0, ':');
d = day.size();
hour = time.substr( d+1, ':');
h = hour.size();
min = time.substr( h+1, ':');
m = min.size();
sec = time.substr( m);
ss= time.size();
}
// initialize hours, minutes and seconds
if (count == 2) {
hour = time.substr( 0, ':');
h = hour.size();
min = time.substr( h+1, ':');
m = min.size();
sec = time.substr( m);
ss = time.size();
}
// initialize minutes and seconds
if (count == 1) {
min = time.substr(0, ':');
m = min.size();
sec = time.substr( m );
ss = time.size();
}
// convert the strings to integers
stringstream buffer(sec);
buffer >> seconds;
stringstream buffer2(min);
buffer2>> minutes;
stringstream buffer3(hour);
buffer3 >> hours;
stringstream buffer4(day);
buffer4 >> days;
感谢您的帮助。
也许,像这样?
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main() {
string time_str = "1:13:0:59";
istringstream iss(time_str);
string temp;
int days = 0, hours = 0, minutes = 0, seconds = 0;
size_t ndelims = count(time_str.begin(), time_str.end(), ':');
int count = 0;
if (ndelims == 3) {
while(getline(iss, temp, ':')) {
cout << atoi(temp.c_str()) << endl;
if (count == 0) {
days = atoi(temp.c_str());
}
else if(count == 1) {
hours = atoi(temp.c_str());
}
else if(count == 2) {
minutes = atoi(temp.c_str());
}
else if (count == 3) {
seconds = atoi(temp.c_str());
}
count = count + 1;
}
cout << days << " " << hours << " " << minutes << " " << seconds << endl;
}
else if (ndelims == 2) {
while(getline(iss, temp, ':')) {
cout << atoi(temp.c_str()) << endl;
if(count == 0) {
hours = atoi(temp.c_str());
}
else if(count == 1) {
minutes = atoi(temp.c_str());
}
else if (count == 2) {
seconds = atoi(temp.c_str());
}
count = count + 1;
}
cout << days << " " << hours << " " << minutes << " " << seconds << endl;
}
else if(ndelims == 1) {
while(getline(iss, temp, ':')) {
cout << atoi(temp.c_str()) << endl;
if(count == 0) {
minutes = atoi(temp.c_str());
}
else if (count == 1) {
seconds = atoi(temp.c_str());
}
count = count + 1;
}
cout << days << " " << hours << " " << minutes << " " << seconds << endl;
}
return 0;
}
这通过计算字符串中(':')
的分隔符数量并将其分解为其组成标记(这将是找到的分隔符数量 + 1)来工作。
然后,意识到最右边的标记总是秒,下一个最右边的标记是分钟,等等,我们可以像上面一样编写代码,这给出了你想要的解决方案。
使用 strtok 将字符串分隔为:"。这适用于字符数组。所以你将不得不使用一些哄骗。
http://www.cplusplus.com/reference/cstring/strtok/
http://www.cplusplus.com/reference/string/string/c_str/
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- C++为构建时间获取QDateTime的可靠方法
- 从持续时间构造std::chrono::system_clock::time_point
- 在没有太多条件句的情况下,我如何避免被零除
- 基于多个条件处理地图中的所有元素
- 条件constexpr函数
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 无论条件是否为true,if总是在c++中执行
- 使用简单类型列表实现的指数编译时间.为什么
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 如何在 python 交易模型中创建功能时间条件
- 在线程中等待一段时间,或者直到条件发生
- C++ 在字符串中分解时间的条件
- 你如何做一个 for 循环作为一段时间的条件
- 是否值得在任何地方使用策略而不是条件(需要运行时检查来编译时间)
- 有条件的大平面数组遍历和令人惊讶的短循环执行时间
- 如何在UML活动图中表示编译时间条件
- 在O(nlog(n))时间内对该条件进行C++检查
- 我必须检查数组的所有元素是否都等于 1.并将其用作一段时间的条件..我该怎么做