list of #'s in a string --> int array

For example, I have a char array/string:
"20,1,1,12,21,11,23,2,2,21,1,1,0,30,0,0,0,1,1,1,
2,3,3,13,1,1,3,3,21,11,1,22,3,2,12,12,13,2,2,2,
32,3,33,3,33,23,32,31,1,21,2,2,3,1,0,1,2,3,3,3..." and so on.

I want to take each of these numbers, separated by a comma, and put them into an int array. First, is it possible to have double-digit numbers in an int array? It seems likely that it is possible, but then I've never actually seen it happen, so I'm not sure. Second, how can I do the conversion?

I began by going through the array/string and removing the commas. However I then realized that the commas would be necessary to tell the difference between single- and double-digit numbers. So I figure there must be a way to go step-by-step through the array/string, stopping at each comma, and insert the numbers before it into the int array. Probably with a for() loop and something to check if the char is a comma... if not, append it to a string... if so, assign the string to the corresponding element in the int array, clear the string, and continue... something along those lines.

This is about as far as I got. I figured I could use a little guidance here because this is all new territory for me, so here I am. I'm going to get to work trying to figure it out, but in case I can't, any help from you would be very much appreciated. :)
Actually, I was able to do it without any of that ^-^'
Forgot that it was actually a vector I wanted to copy the numbers to, but that wasn't much of a problem once I figured it out.

1
2
3
4
5
6
7
8
9
10
11
char mData[] = {/*list of numbers*/};
string eValue; // element Value

for(int i = 0; i < strlen(mData); i++)			// run through all the elements in mData[]
{	if(mData[i]!=',')				// if the element isn't a comma,
		eValue.push_back(mData[i]);		// append it to the string.
	else						// otherwise,
	{	int element = atoi(eValue.c_str());	// convert the string to an integer,
		eValue.clear();				// clear the string, and
		vector.push_back(element);		// append the integer to the vector.
}	}


This works, though so far I'm running into a bit of trouble getting it to copy ALL the numbers to the vector. It had something to do with how I was allocating mData at first (by the number of numbers and not of characters), but now it's just the very last element that isn't going through. Anyways, that's a different story. Thanks for the pointers R0mai ^-^
Consider another approach:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
 
int main()
{
    // source
    char mData[] = "20,1,1,12,21,11,23,2,2,21,1,1,0,30,0,0,0,1,1,1,"
                   "2,3,3,13,1,1,3,3,21,11,1,22,3,2,12,12,13,2,2,2,"
                   "32,3,33,3,33,23,32,31,1,21,2,2,3,1,0,1,2,3,3,3";
    // destination
    vector<int> container;
 
    // tokenize string using ',' as a delimiter
    string token;
    stringstream sin( mData );
    while( getline( sin, token, ',' ) ) 
    {   
        // convert each string token into an integer
        int number;
        stringstream converter( token );
        converter >> number;
        // add integer to container
        container.push_back( number );
    }   
 
    // dump contents of container to stdout
    copy( container.begin(), container.end(), ostream_iterator<int>( cout, " " ) );
    cout << endl;
    
    return 0;
}

This isn't the most efficient way but it is fairly clean and straight forward.
Last edited on
Topic archived. No new replies allowed.