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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page