Taking the months out of a string that is within a vector of pairsHow to find out if an item is present in a std::vector?vector< pair<aStruct*,int> > not possible?C++ : push_back a map<string, int> into a vector<map<string, int> > via an iterator?Vector of pairs to mapC++ Copy data from float vector to a vector of float pairsfind inside a class if an element exists within a vector of pairsPrinting the elements of vectorPrinting out sum of each pair of vector elements - C++Removing elements by index in vector pairspush_back vs emplace_back to a std::vector<std::string>

Is there a reason to prefer HFS+ over APFS for disk images in High Sierra and/or Mojave?

Is there a distance limit for minecart tracks?

Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Language involving irrational number is not a CFL

Pre-Employment Background Check With Consent For Future Checks

How to make money from a browser who sees 5 seconds into the future of any web page?

Isometric embedding of a genus g surface

Alignment of six matrices

Deciphering cause of death?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

What is the smallest number n> 5 so that 5 ^ n ends with "3125"?

Has the laser at Magurele, Romania reached a tenth of the Sun's power?

Echo with obfuscation

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Anime with legendary swords made from talismans and a man who could change them with a shattered body

Mimic lecturing on blackboard, facing audience

What is the meaning of "You've never met a graph you didn't like?"

Do I have to know the General Relativity theory to understand the concept of inertial frame?

Why the "ls" command is showing the permissions of files in a FAT32 partition?

Overlapping circles covering polygon

How do I Interface a PS/2 Keyboard without Modern Techniques?

Is there anyway, I can have two passwords for my wi-fi

Why the various definitions of the thin space ,?



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


How to find out if an item is present in a std::vector?vector< pair<aStruct*,int> > not possible?C++ : push_back a map<string, int> into a vector<map<string, int> > via an iterator?Vector of pairs to mapC++ Copy data from float vector to a vector of float pairsfind inside a class if an element exists within a vector of pairsPrinting the elements of vectorPrinting out sum of each pair of vector elements - C++Removing elements by index in vector pairspush_back vs emplace_back to a std::vector<std::string>













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






  • 1





    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






  • 1





    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 std-pair






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 21:53









Brian Tompsett - 汤莱恩

4,2421339102




4,2421339102










asked Nov 19 '18 at 18:43









SnorrlaxxxSnorrlaxxx

13611




13611












  • 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






  • 1





    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






  • 1





    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




1




1





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

95.8k16135208




95.8k16135208












  • 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.




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

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived