从成对向量中的字符串中取出月份

Taking the months out of a string that is within a vector of pairs

我有一对日期和付款向量,如下所示:

std::vector<std::pair<std::string, double>> payments = { {"8/18", 0.0}, {"7/18", 771.98}, {"6/18", 0.0}, {"5/18", 771.98},
                                                    {"4/18", 771.98}, {"3/18", 771.98}, {"2/18", 0.0}, {"1/18", 3859.90},
                                                    {"12/17", 771.98}, {"11/17", 0.0}, {"10/17", 1543.96}, {"9/17", 771.98} };

我想从每个第一个元素中取出月份并将其放入一个整数向量中,即

 payment_months = [8,7,6,5,4,3,2,1,12,11,10,9]

我试过这样做:

std::vector<int> paymentMonths;
for (auto it : payments)
{
    paymentMonths.push_back(it.first[0] - '0');
}

这给了我

8 7 6 5 4 3 2 1 1 1 1 9

所以问题是当我到达 12 月、11 月和 10 月时。有谁知道如何解决这个问题?

因为您的某些月份有不止一位数字代表它们,您需要做的是获取日期字符串的子字符串,其中只有月份部分,然后您可以使用 [= 将其转换为整数11=]。那会让你的厕所看起来像

std::vector<int> paymentMonths;
for (auto it : payments)
{
    paymentMonths.push_back(std::stoi(it.first.substr(0, it.first.find("/"))));
}