使用分隔符C ++将字符串解析为整数

时间:2017-12-16 00:17:10

标签: c++ parsing int delimiter

我正在尝试制作一个callrate计算器,其输入将按如下方式完成:hh:mm

在此之后我想将该字符串解析为两个整数,只有':'作为分隔符。我得到的这个解决方案似乎只适用于空间,但我希望分隔符是冒号,而不是空格。甚至有可能这样做吗?

#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {

    string input;
    getline(cin, input);
    istringstream is(input);
    int hours, minutes; 

    is >> hours>> minutes;

    cout << timmar << endl;
    cout << minuter << endl;
}

2 个答案:

答案 0 :(得分:1)

将分隔符字符读入另一个变量。

char colon;

is >> hours >> colon >> minutes;
if (colon != ':') {
    cout << "Incorrect delimiter: " << colon << '\n';
}

答案 1 :(得分:0)

我没有使用默认分隔符,而是使用自定义操纵器来专门处理分隔符:

std::istream& colon(std::istream& in) {
    if (in.peek() != ':' || std::isspace(in.ignore().peek())) {
        in.setstate(std::ios_base::failbit);
    }
    return in;
}
// ...
int hours, minutes;
if (in >> hours >> colon >> minutes) {
    // process the input
}

使用此操纵器可避免冒号附近的空格被视为有效:正常输入操作以跳过空格开始。简单地将分隔符读入变量将允许分隔符前面的空格,并允许任何非空格字符作为分隔符。在分隔符后直接进行读取将再次允许在分隔符后面的空格。使用上面的colon操纵器会将这两种情况都变成错误。 ......当然,总是需要在阅读后检查之前使用结果检查输入是否成功。

对同一问题采用完全不同的方法是重新定义流的空格的表示法。这里的想法是利用流std::localeimbue()std::locale来考虑所需的分隔符,例如:作为唯一的“空白”。下面是一个展示这个想法的完整例子。它使用单独的std::istream和自定义std::locale来避免更改std::cin的工作方式。但是,它仍然使用std::cin流缓冲区,即它从与std::cin相同的源中读取。

#include <algorithm>
#include <locale>

struct myctype_base {
    std::ctype<char>::mask table[std::ctype<char>::table_size];
    myctype_base(unsigned char c) {
        table[c] |= std::ctype_base::space;
    }
};
struct myctype
    : private myctype_base
    , std::ctype<char> {
public:
    myctype(char delimiter)
        : myctype_base(delimiter)
        , std::ctype<char>(myctype_base::table) {
    }
};

int main() {
    std::locale loc(std::locale(), new myctype(':'));
    std::istream in(std::cin.rdbuf());
    in.imbue(loc);

    int hours, minutes;
    if (in >> hours >> minutes) {
        std::cout << "read hours=" << hours << " minutes=" << minutes << "\n";
    }
    else {
        std::cout << "failed to read input\n";
    }
}