On 25 Dec 2005 12:20:26 -0800, "Al" <alberto.bietti@gmail.com> wrote:
[color=blue]
>What is the problem with this code? It always crashes at the
>'push_back'. I found that thanks to debugging. Any Ideas?
>Other question: what is a faster way to convert a string to an integer?
>Is there a built-in function to do that?[/color]
Yes. You want either std::stringstream or the CRT strtod() function.
The latter is better for sanity-checking of input. The former is
easier to use.
[color=blue]
>Here's the code:
>
>#include <iostream>
>#include <fstream>
>#include <vector>
>#include <string>
>using namespace std;
>
>int stringToInt(string text)[/color]
Since you do not change "text", it should be passed by const
reference. Otherwise, you are copying it unnecessarily.
[color=blue]
>{
> int integer=0, temp=1;[/color]
It's considered bad style by many people to declare more than one
variable on the same line.
[color=blue]
> for (int i=text.size()-1, j=1;i>=0;i--, j++)[/color]
What happens when text.size() == 0?
What happens when text.size() > 10?
What happens when text holds non-digit characters?
What happens when text is negative?
Where is j used in the loop?
[color=blue]
> {
> switch (text[i]) {[/color]
This is not necessarily portable code because you are making
assumptions about the internal representation of the particular
character encoding used. Better to use something like this:
case '1':
integer += temp; break;
case '2':
integer += 2 * temp; break;
// etc.
Note the single quotes used.
[color=blue]
> case 49:
> integer += temp; break;
> case 50:
> integer += 2 * temp; break;
> case 51:
> integer += 3 * temp; break;
> case 52:
> integer += 4 * temp; break;
> case 53:
> integer += 5 * temp; break;
> case 54:
> integer += 6 * temp; break;
> case 55:
> integer += 7 * temp; break;
> case 56:
> integer += 8 * temp; break;
> case 57:
> integer += 9 * temp; break;
> default: break;
> }
> temp *= 10;
> }
> return integer;
>}
>
>template<class T>
>void print(vector<T> v)
>{
> for (int i=0; i<v.size(); i++)[/color]
size() returns unsigned. It's a very bad mistake to compare signed
with unsigned values, even if your compiler lets you do it. Turn up
your warning level so that you at least get a warning about it here.
[color=blue]
> cout << i <<" : "<<v[i]<<endl;
>}
>
>int main()
>{
> ifstream is("D:/dataa.txt");
> vector<vector<int> > theMat;
> string data = "";
> char c;
> int number;
> for (int i=0;;i++)
> {
> while (is.get(c)) {[/color]
The std::getline() function is much better than reading a file
character-for-character.
[color=blue]
> if (c == '\t') theMat[i].push_back(stringToInt(data));
>//here is the problem: the program crashes[/color]
theMat is a vector of vectors. It was never initialized. Therefore, it
has no elements, yet you use element 0, therefore it crashes.
[color=blue]
> else if (c == '\n') break;
> else data += c;
> }
> }
> print(theMat[0]);
> /*string test = "132435";
> cout <<stringToInt(test); */
>
> system("pause");
> return 0;
>}[/color]
--
Bob Hairgrove
NoSpamPlease@Home.com