Could you spot the error in my code?

Hi,

I'm trying to convert a 2d array given in the form of strings to integer 2d arrays.

ie, the array will be given as a vector of strings and i need to get it in 2d int vector.

But, the last column of the 2d array is missing in the converted matrix.

I suspect the error is in the following code, but unable to spot any. Please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector< vector<int> > IntMatrix;
			vector<string>::iterator vsitr;
			string::iterator sitr;
			
			for(vsitr=strmat.begin();vsitr != strmat.end(); vsitr++)
			{
				vector<int> row;
				for(sitr=vsitr->begin(); sitr+1  != vsitr->end(); sitr = sitr +2)
				{
					row.push_back(*(sitr) - '0');
				}
				IntMatrix.push_back(row);
			}
			return IntMatrix;


specifically at
1
2
3
4
for(sitr=vsitr->begin(); sitr+1  != vsitr->end(); sitr = sitr +2)
{
        row.push_back(*(sitr) - '0');
}


The entire code is below if you need to check it.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class TallPeople
{
	public: 
		vector <int>  getPeople(vector <string> people)
		{
			vector<int> result;
			vector<int> inter;
			vector< vector <int> > matrix = convert(people);
			
			//calculate tallest of the shortest
			vector< vector<int> >::iterator vvitr;
			for(vvitr=matrix.begin();vvitr != matrix.end(); vvitr++)
			{
				vector<int> vint;
				vint.assign(vvitr->begin(), vvitr->end());
				inter.push_back(*min_element(vint.begin(),vint.end()));
			}
			result.push_back(*max_element(inter.begin(),inter.end()));
			
			//calculate shortest of the tallest
			inter.erase(inter.begin(), inter.end());
			for(int i=0 ;i < matrix[0].size(); i++)
			{
				int maxi=0;
				for(int j=0; j < matrix.size();j++)
					maxi = max(maxi, matrix[j][i]);
				inter.push_back(maxi);		
			}
				
			result.push_back(*(min(inter.begin(), inter.end())));
			return inter;
		}
	private:
		vector< vector<int> > convert(vector <string> strmat)
		{
			vector< vector<int> > IntMatrix;
			vector<string>::iterator vsitr;
			string::iterator sitr;
			
			for(vsitr=strmat.begin();vsitr != strmat.end(); vsitr++)
			{
				vector<int> row;
				for(sitr=vsitr->begin(); sitr+1  != vsitr->end(); sitr = sitr +2)
				{
					row.push_back(*(sitr) - '0');
				}
				IntMatrix.push_back(row);
			}
			return IntMatrix;
		}	
	
};


The input format is as below
Each element of people will be a single space-delimited list of positive integers such that:
1) Each positive integer is between 1 and 1000 inclusive with no extra leading zeros.
2) Each element contains the same number of integers.
3) Each element contains at least 2 positive integers.
4) Each element does not contain leading or trailing whitespace.


Thanks
Would you write an example string (which there are in the string vector in convert fnc)?

You said:
2) Each element contains the same number of integers.

But this command row.push_back(*(sitr) - '0'); convert only individual characters and not entirely number.

I think a string for example "1000" then *(sitr) - '0' command makes 4 individual integers which contains 1, 0, 0, 0 values.




This is problem from topcoder site. You can see the question here and sample inputs at

http://www.topcoder.com/stat?c=problem_statement&pm=2923&rd=5854.

And I haven't thought about the point you mentioned, i considered only one digit numbers. So, my solution will not scale I guess. I will need to rewrite my code.

I solved above mentioned issue, by rewriting my code as below

1
2
3
4
5
for(sitr=vsitr->begin(); sitr  != vsitr->end(); sitr++)
{
       if( *sitr != ' ')
                   row.push_back(*(sitr) - '0');
}


but still dont get, why above code didnot work.

A sample given in the site is pasted below, in case youd dont have topcoder login

{"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20", "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20"}
You can convert the numbers from string to integer very simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
#include <sstream>

using namespace std;


int main(int argc, char *argv[])
{
    string s = "1234  11  38 1234567";
    stringstream ss;
    ss << s;
    int n = 0;

    do
    {
         ss >> n;
         cout << n << endl;
    }while( ss.good() );

    return 0;
}


You get this outcome (in integer variables of course):

1234
11
38
1234567
Hi,

thanks for this intput. I never used sstream before. So couldn't think this way. Quite easy i guess.



Last edited on
Topic archived. No new replies allowed.