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












1














I have a vector pair of dates and payments that looks like this:



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} };


I want to take the months out of each first element and put it into a vector of ints i.e.




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



I tried doing this:



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


This gives me



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


So the problem is when I get to the the months of December, November, and October. Does anyone know how to fix this?










share|improve this question
























  • Can you be more specific about your problem please. Post a Minimal, Complete, and Verifiable example that reproduces the problem you're stuck with as required here.
    – πάντα ῥεῖ
    Nov 19 '18 at 18:46










  • @πάνταῥεῖ Edited with an example
    – Snorrlaxxx
    Nov 19 '18 at 18:48










  • Why are you storing dates as a string rather than a Date/Time class that can deal with all the nitty-gritty of time?
    – Jesper Juhl
    Nov 19 '18 at 18:51










  • Break the problem down. The issue is not with vector, as you already are getting the string correctly. The issue is one of taking a string delimited by a slash and getting the first token.
    – PaulMcKenzie
    Nov 19 '18 at 18:52












  • Why not using a map to get a month-int from a month-string ?
    – Damien
    Nov 19 '18 at 18:53
















1














I have a vector pair of dates and payments that looks like this:



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} };


I want to take the months out of each first element and put it into a vector of ints i.e.




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



I tried doing this:



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


This gives me



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


So the problem is when I get to the the months of December, November, and October. Does anyone know how to fix this?










share|improve this question
























  • Can you be more specific about your problem please. Post a Minimal, Complete, and Verifiable example that reproduces the problem you're stuck with as required here.
    – πάντα ῥεῖ
    Nov 19 '18 at 18:46










  • @πάνταῥεῖ Edited with an example
    – Snorrlaxxx
    Nov 19 '18 at 18:48










  • Why are you storing dates as a string rather than a Date/Time class that can deal with all the nitty-gritty of time?
    – Jesper Juhl
    Nov 19 '18 at 18:51










  • Break the problem down. The issue is not with vector, as you already are getting the string correctly. The issue is one of taking a string delimited by a slash and getting the first token.
    – PaulMcKenzie
    Nov 19 '18 at 18:52












  • Why not using a map to get a month-int from a month-string ?
    – Damien
    Nov 19 '18 at 18:53














1












1








1







I have a vector pair of dates and payments that looks like this:



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} };


I want to take the months out of each first element and put it into a vector of ints i.e.




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



I tried doing this:



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


This gives me



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


So the problem is when I get to the the months of December, November, and October. Does anyone know how to fix this?










share|improve this question















I have a vector pair of dates and payments that looks like this:



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} };


I want to take the months out of each first element and put it into a vector of ints i.e.




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



I tried doing this:



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


This gives me



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


So the problem is when I get to the the months of December, November, and October. Does anyone know how to fix this?







c++ vector pair






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 18:48







Snorrlaxxx

















asked Nov 19 '18 at 18:43









SnorrlaxxxSnorrlaxxx

14311




14311












  • Can you be more specific about your problem please. Post a Minimal, Complete, and Verifiable example that reproduces the problem you're stuck with as required here.
    – πάντα ῥεῖ
    Nov 19 '18 at 18:46










  • @πάνταῥεῖ Edited with an example
    – Snorrlaxxx
    Nov 19 '18 at 18:48










  • Why are you storing dates as a string rather than a Date/Time class that can deal with all the nitty-gritty of time?
    – Jesper Juhl
    Nov 19 '18 at 18:51










  • Break the problem down. The issue is not with vector, as you already are getting the string correctly. The issue is one of taking a string delimited by a slash and getting the first token.
    – PaulMcKenzie
    Nov 19 '18 at 18:52












  • Why not using a map to get a month-int from a month-string ?
    – Damien
    Nov 19 '18 at 18:53


















  • Can you be more specific about your problem please. Post a Minimal, Complete, and Verifiable example that reproduces the problem you're stuck with as required here.
    – πάντα ῥεῖ
    Nov 19 '18 at 18:46










  • @πάνταῥεῖ Edited with an example
    – Snorrlaxxx
    Nov 19 '18 at 18:48










  • Why are you storing dates as a string rather than a Date/Time class that can deal with all the nitty-gritty of time?
    – Jesper Juhl
    Nov 19 '18 at 18:51










  • Break the problem down. The issue is not with vector, as you already are getting the string correctly. The issue is one of taking a string delimited by a slash and getting the first token.
    – PaulMcKenzie
    Nov 19 '18 at 18:52












  • Why not using a map to get a month-int from a month-string ?
    – Damien
    Nov 19 '18 at 18:53
















Can you be more specific about your problem please. Post a Minimal, Complete, and Verifiable example that reproduces the problem you're stuck with as required here.
– πάντα ῥεῖ
Nov 19 '18 at 18:46




Can you be more specific about your problem please. Post a Minimal, Complete, and Verifiable example that reproduces the problem you're stuck with as required here.
– πάντα ῥεῖ
Nov 19 '18 at 18:46












@πάνταῥεῖ Edited with an example
– Snorrlaxxx
Nov 19 '18 at 18:48




@πάνταῥεῖ Edited with an example
– Snorrlaxxx
Nov 19 '18 at 18:48












Why are you storing dates as a string rather than a Date/Time class that can deal with all the nitty-gritty of time?
– Jesper Juhl
Nov 19 '18 at 18:51




Why are you storing dates as a string rather than a Date/Time class that can deal with all the nitty-gritty of time?
– Jesper Juhl
Nov 19 '18 at 18:51












Break the problem down. The issue is not with vector, as you already are getting the string correctly. The issue is one of taking a string delimited by a slash and getting the first token.
– PaulMcKenzie
Nov 19 '18 at 18:52






Break the problem down. The issue is not with vector, as you already are getting the string correctly. The issue is one of taking a string delimited by a slash and getting the first token.
– PaulMcKenzie
Nov 19 '18 at 18:52














Why not using a map to get a month-int from a month-string ?
– Damien
Nov 19 '18 at 18:53




Why not using a map to get a month-int from a month-string ?
– Damien
Nov 19 '18 at 18:53












1 Answer
1






active

oldest

votes


















2














Because some of your months have more than one digit representing them what you need to do is get the sub string of the date string that just has the month part, and then you can convert that to a integer using stoi. That would make you loo look like



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





share|improve this answer





















  • Very nice thank you
    – Snorrlaxxx
    Nov 19 '18 at 18:50






  • 1




    @Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
    – NathanOliver
    Nov 19 '18 at 18:56













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380830%2ftaking-the-months-out-of-a-string-that-is-within-a-vector-of-pairs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Because some of your months have more than one digit representing them what you need to do is get the sub string of the date string that just has the month part, and then you can convert that to a integer using stoi. That would make you loo look like



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





share|improve this answer





















  • Very nice thank you
    – Snorrlaxxx
    Nov 19 '18 at 18:50






  • 1




    @Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
    – NathanOliver
    Nov 19 '18 at 18:56


















2














Because some of your months have more than one digit representing them what you need to do is get the sub string of the date string that just has the month part, and then you can convert that to a integer using stoi. That would make you loo look like



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





share|improve this answer





















  • Very nice thank you
    – Snorrlaxxx
    Nov 19 '18 at 18:50






  • 1




    @Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
    – NathanOliver
    Nov 19 '18 at 18:56
















2












2








2






Because some of your months have more than one digit representing them what you need to do is get the sub string of the date string that just has the month part, and then you can convert that to a integer using stoi. That would make you loo look like



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





share|improve this answer












Because some of your months have more than one digit representing them what you need to do is get the sub string of the date string that just has the month part, and then you can convert that to a integer using stoi. That would make you loo look like



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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:49









NathanOliverNathanOliver

87.4k15120180




87.4k15120180












  • Very nice thank you
    – Snorrlaxxx
    Nov 19 '18 at 18:50






  • 1




    @Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
    – NathanOliver
    Nov 19 '18 at 18:56




















  • Very nice thank you
    – Snorrlaxxx
    Nov 19 '18 at 18:50






  • 1




    @Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
    – NathanOliver
    Nov 19 '18 at 18:56


















Very nice thank you
– Snorrlaxxx
Nov 19 '18 at 18:50




Very nice thank you
– Snorrlaxxx
Nov 19 '18 at 18:50




1




1




@Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
– NathanOliver
Nov 19 '18 at 18:56






@Snorrlaxxx Your welcome. Do note that I did not include error checking in this. If your strings might not be in the form of "xx/yy" then this could cause an issue.
– NathanOliver
Nov 19 '18 at 18:56




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380830%2ftaking-the-months-out-of-a-string-that-is-within-a-vector-of-pairs%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter