
-----------------------------------
jrcdude
Mon Feb 21, 2011 12:30 am

C++ String Handling
-----------------------------------
For this post I will be referring to the CCC 2010 Junior Problem 4 question.

For my implementation of receiving the data to process, I had to use some very hacky techniques.  I would like to know what the proper way to do this would be.

Example:

string input = "7 3 4 6 4 5 7 5";
int out[10];

I did the follow:

(int)input.at(0) - 48    //results in number of integers following

loop through the string
if((int)input.at(x) != 32)
out[x] = (int)input.at(x) - 48

if(input.at(x+1) != 32)
out[x] *= 10
out[x] += (int)input.at(x + 1) - 48

So as you can see it gets messy with the (int) conversion, etc.

I know in Java it would be much better to simply use a tokenizer with a space as a delimiter, but how would one do something such as this easily in C++, or is there a better technique for input such as this.

Thanks,
Jason

-----------------------------------
Tony
Mon Feb 21, 2011 12:46 am

RE:C++ String Handling
-----------------------------------
Isn't the iostream library the first thing that is taught for C++ (it being necessary for Hello World)?


McAwesome:/tmp $ cat test.cpp 
#include 

int main(){
  int num_inputs, num;

  std::cin >> num_inputs;
  for(int i=0; i < num_inputs; i++){
    std::cin >> num;
    std::cout 