Jump to content

Hello,

 

Lets say I have an input string that looks like this "3-5-2", without the quotes, and I need to separate it to 3 integers a b c so a=3, b=5, c=2.

 

I would search google if I knew what keywords to look for. This might be pretty basic for most programmers but knowing this would solve a lot of stuff for me, thanks!

 

 

Link to comment
https://linustechtips.com/topic/306084-c-string-to-multiple-integers/
Share on other sites

Link to post
Share on other sites

Hello,

 

Lets say I have an input string that looks like this "3-5-2", without the quotes, and I need to separate it to 3 integers a b c so a=3, b=5, c=2.

 

I would search google if I knew what keywords to look for. This might be pretty basic for most programmers but knowing this would solve a lot of stuff for me, thanks!

Right, so I Googled a bit and found this:

http://stackoverflow.com/questions/15324640/in-c-how-do-i-split-a-string-into-multiple-integers

I have no experience with C++, so couldn't help you out further.

 

(I Googled "string to multiple ints c++")

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Don't know about C++ (I'm more at home with Java/C#), but my idea is to split the string to a string array (use the '-' as a split parameter) and then parse the string array to an int array... Not really elegant, but it's an idea to think about.

Asrock 890GX Extreme 3 - AMD Phenom II X4 955 @3.50GHz - Arctic Cooling Freezer XTREME Rev.2 - 4GB Kingston HyperX - AMD Radeon HD7850 - Kingston V300 240GB - Samsung Spinpoint F3 1TB - Chieftec APS-750 - Cooler Master HAF912 PLUS


osu! profile

Link to post
Share on other sites

Right, so I Googled a bit and found this:

http://stackoverflow.com/questions/15324640/in-c-how-do-i-split-a-string-into-multiple-integers

I have no experience with C++, so couldn't help you out further.

 

(I Googled "string to multiple ints c++")

 

 

Don't know about C++ (I'm more at home with Java/C#), but my idea is to split the string to a string array (use the '-' as a split parameter) and then parse the string array to an int array... Not really elegant, but it's an idea to think about.

I found this http://stackoverflow.com/questions/11719538/how-to-use-stringstream-to-separate-comma-separated-strings

 

Changed the answer's code a little and used stoi to convert strings to int. I know there must be a better soloution but this works for me. Thanks.

 

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×