By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

vector 'push_back' problem

P: n/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[i]) {
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[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)) {
if (c == '\t') theMat[i].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;
}

Dec 25 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 25 Dec 2005 12:20:26 -0800, "Al" <al************@gmail.com> wrote:
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[i]) {
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[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)) {
The std::getline() function is much better than reading a file
character-for-character.
if (c == '\t') theMat[i].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;
}


--
Bob Hairgrove
No**********@Home.com
Dec 25 '05 #2

P: n/a

Al wrote in message
<11**********************@o13g2000cwo.googlegroups .com>...
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[s] are not out-of-range
// you should use the at() to access the vector[s].

// 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]
--
Bob R
POVrookie
Dec 26 '05 #3

P: n/a
"BobR" <Re***********@worldnet.att.net> wrote in message
news:Uk*********************@bgtnsc04-news.ops.worldnet.att.net...
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);


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.
Dec 26 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.