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>
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
add a comment |
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
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
add a comment |
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
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
c++ vector std-pair
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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("/"))));
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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("/"))));
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
add a comment |
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("/"))));
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
add a comment |
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("/"))));
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("/"))));
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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