vector 'push_back' problem

A

Al

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?
Here's the code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int stringToInt(string text)
{
int integer=0, temp=1;
for (int i=text.size()-1, j=1;i>=0;i--, j++)
{
switch (text) {
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++)
cout << i <<" : "<<v<<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)) {
if (c == '\t') theMat.push_back(stringToInt(data));
//here is the problem: the program crashes
else if (c == '\n') break;
else data += c;
}
}
print(theMat[0]);
/*string test = "132435";
cout <<stringToInt(test); */

system("pause");
return 0;
}
 
B

Bob Hairgrove

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?

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.
Here's the code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int stringToInt(string text)

Since you do not change "text", it should be passed by const
reference. Otherwise, you are copying it unnecessarily.
{
int integer=0, temp=1;

It's considered bad style by many people to declare more than one
variable on the same line.
for (int i=text.size()-1, j=1;i>=0;i--, j++)

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?
{
switch (text) {


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.
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++)

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.
cout << i <<" : "<<v<<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)) {


The std::getline() function is much better than reading a file
character-for-character.
if (c == '\t') theMat.push_back(stringToInt(data));
//here is the problem: the program crashes


theMat is a vector of vectors. It was never initialized. Therefore, it
has no elements, yet you use element 0, therefore it crashes.
else if (c == '\n') break;
else data += c;
}
}
print(theMat[0]);
/*string test = "132435";
cout <<stringToInt(test); */

system("pause");
return 0;
}
 
B

BobR

Al wrote in message
What is the problem with this code? It always crashes at the
'push_back'. I found that thanks to debugging. Any Ideas?

// You need to initialize the vector with a size:
// here's one way:
size_t rows(480);
size_t cols(640);
vector<vector<int> > theMat( rows, cols);

size_t row(4);
size_t col(6);
theMat.at(row).at(col) = 43; // set 5th row, 7th col to 43
// unless you are positive the index are not out-of-range
// you should use the at() to access the vector.

// another way to do it
vector<vector<int> > theMat;
for(size_t i(0); /* you need a way out */ ; ++i){
std::vector<int> tmp;
theMat.push_back( tmp ); // each pass add another row.
while (is.get(c)) {
if (c == '\t') theMat.at( i ).push_back( FromString<int>(data) );
// ...... fill in data ....
} //while()
if( not is ) break;
} // for()

Other question: what is a faster way to convert a string to an integer?
Is there a built-in function to do that?

There are stringstream ToString() and FromString() templates/methods in some
FAQs.

#include <sstream>
// --- string to any number ---
template<typename T> T FromString(std::string const &str){
std::istringstream is(str); T tmp; is >> tmp; return tmp;
} //FromString()
// -- use it like this --
std::string Str("54321");
int num = FromString<int>(Str);
double num2 = FromString<double>(Str);

Heed Mr. Hairgrove's suggestions.

The comp.lang.c++ FAQ is available at http://www.parashift.com/c++-faq-lite/

[corrections always welcome]
 
J

Jim Langston

BobR said:
There are stringstream ToString() and FromString() templates/methods in
some
FAQs.

#include <sstream>
// --- string to any number ---
template<typename T> T FromString(std::string const &str){
std::istringstream is(str); T tmp; is >> tmp; return tmp;
} //FromString()
// -- use it like this --
std::string Str("54321");
int num = FromString<int>(Str);
double num2 = FromString<double>(Str);

Here is the version I use:

#include <sstream>
template<typename T, typename F > T StrmConvert( F from )
{
std::stringstream temp;
temp << from;
T to = T();
temp >> to;
return to;
}

The advantage of this version is that you can convert from/to std::string
without problems using your same syntax.

int num = 12345;
std::string str("67890");

std::string str2 = StrmConvert<std::string>( num );
int num2 = StrmConvert<int>( str );

You don't have to specify the type to convert from, the template figures
that out itself (somehow I'm not really positive on the mechanics). You
just have to specify what you are converting to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,829
Latest member
KimberAlli

Latest Threads

Top