1

I'm wondering if there is a way to avoid using a temporary string in this case:

tempString = inputString.substr(commaLast,commaCurrent-1);
yPos = strtod(tempString.c_str(), NULL);

There is no way to use the substr command and returning a c_str without first storing the substring in a temp string (supposing I dont want to modify the original string).

  • Are you doing anything else with inputString? The fact that you're using strtod and c_str in the first place suggests to me that you might get a better solution using a std::stringstream instead. – Ben Cottrell Apr 09 '12 at 20:44
  • yes, I'm splitting the string based on several conditions. I'm trying to parse a SVG line. I've looked at std::stringsteam, but I thought this was only good for whitespace splitting – jimspree1200 Apr 09 '12 at 20:56
  • the >> operator is whitespace delimited by default, but you don't have to let that stop you; a stringstream will let you handle string data in the same way as input from std::cin, so before/after a read, you can skip over subsequent characters using `istream::ignore( /* ... */ )` up to and including any delimiter of your choosing. (I don't know much about the the SVG format, so that may or may not be useful to you - but I find it works fine for simple delimited data) – Ben Cottrell Apr 09 '12 at 21:28
  • intere-string! thanks for that. Unfortunately for the SVG format sometimes I need to read the delimiter too. For example coordinates can be `1,2,-3,-4,5` or `1,2-3-4,5` for "efficiency". So I actually need to read the delimiter `-` sometimes as a value too (and not skip it). I hope that makes sense. – jimspree1200 Apr 09 '12 at 22:11

1 Answers1

0

Have you tried the following?

yPos = strtod(inputString.substr(commaLast,commaCurrent-1).c_str(), NULL);
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
  • Arg! I could have sworn that I did! I guess I was trying: yPos = strtod(inputString.c_str().substr(commaLast, commaCurrent-1), NULL); – jimspree1200 Apr 09 '12 at 20:50