Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!

We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.

std::string and erase() to get data I need from string

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 MARS_999   Members   -  Reputation: 1288


Posted 28 December 2012 - 10:23 PM

I have a string as follows




I want to grab each of the three values separately, so I can put them into a int variable.


I am not sure what std lib can do this for me?




#2 ultramailman   Prime Members   -  Reputation: 1581


Posted 28 December 2012 - 10:55 PM

You can use sscanf from stdio. It's like scanf, except it's used on a char* instead of stdin.


sscanf("640x480x32", "%dx%dx%d", &num1, &num2, &num3);


You can also write your own function to parse it.


But I have a feeling that it is possible to parse it with just string stream and a few << operators.

Edited by ultramailman, 28 December 2012 - 10:56 PM.

#3 Riztro   Members   -  Reputation: 240


Posted 28 December 2012 - 11:13 PM

Yeah, if you already have the "640x480x32" in a string, you will have to perform tokenization in order to split it up into "Tokens". After that you can extract them as integers. Here is some code to do that:

#include <sstream>
#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
   string u_numbers = "640x480x32";  //unformatted numbers
   string f_numbers; //formatted numbers
   vector <int> numbers; //final numbers in integer form
   stringstream ss;
   for(int i = 0; i < u_numbers.size(); i++)
      if(u_numbers.at(i) == 'x') //check if next character is the delimiter
         ss.put(' ');
         ss.put(u_numbers.at(i)); //Output the unformatted string to stringstream

   std::getline(ss, f_numbers); //Get all the data we just put into the stream and put it into another string

   std::istringstream iss(f_numbers); //create an integer string stream, fill the stream with the string of formatted numbers
   int n;

   while (iss >> n)
      numbers.push_back(n); //pull all the data from the iss and store it as integers in our vector

   for (int i = 0; i < 3; i++) //a little test to see if it works
      cout << numbers[i] << ' ';

return 0;

Edited by Riztro, 28 December 2012 - 11:16 PM.

#4 Trienco   Crossbones+   -  Reputation: 2207


Posted 28 December 2012 - 11:37 PM

If you can rely on your input being formatted like that, just dump it in a stringstream and read it back.

const string res = "640x480x32";
int height, width, depth;
char delimiter;  

stringstream stream(res);
stream >> height >> delimiter >> width >> delimiter >> depth;



You could use getline and define 'x' as a delimiter, but then you get each number as a string and still need to convert to int.

You could use .ignore(1) instead of the dummy character, but it will look horrible to read.


#5 MARS_999   Members   -  Reputation: 1288


Posted 29 December 2012 - 12:01 AM

Thanks all! Love to see so many different approachs!

#6 simast   Members   -  Reputation: 119


Posted 29 December 2012 - 01:54 AM

Another approach (with regular expressions and boost lexical cast):

#include <regex>
#include <boost/lexical_cast.hpp>

std::cmatch matches;
std::regex regExp("\\s{0,}([0-9]+)\\s{0,}x([0-9]+)\\s{0,}x\\s{0,}([0-9]+)\\s{0,}", std::regex_constants::icase);

if (std::regex_match("100X200 x 300", matches, regExp)) {
  unsigned int width = boost::lexical_cast<unsigned int>(matches[1].str());
  unsigned int height = boost::lexical_cast<unsigned int>(matches[2].str());
  unsigned int depth = boost::lexical_cast<unsigned int>(matches[3].str());

Edited by simast, 29 December 2012 - 01:58 AM.

#7 fastcall22   Crossbones+   -  Reputation: 4374


Posted 29 December 2012 - 02:03 AM

With error checking:
struct ScreenResolution {
	int width, height, depth;

istream& operator >> ( istream& stream, ScreenResolution& r ) {
	ScreenResolution temp;
	array<char,2> delim;
	auto pos = stream.tellg();
	if ( (stream >> temp.width >> delim[0] >> temp.height >> delim[1] >> temp.depth) && count(delim.begin(),delim.end(),'x') == 2 )
		r = temp;
	else {
		stream.clear( stream.rdstate() | ios::failbit );
		stream.seekg( pos );
	return stream;

int main() {
	istringstream stream("800x600x32");
	ScreenResolution r;
	if ( stream >> r )
		cout << r.width << 'x' << r.height << 'x' << r.depth << endl;
		cout << "error" << endl;


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.