Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 25th, 2005, 08:35 PM
Al
Guest
 
Posts: n/a
Default vector 'push_back' problem

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;
}

  #2  
Old December 25th, 2005, 09:25 PM
Bob Hairgrove
Guest
 
Posts: n/a
Default Re: vector 'push_back' problem

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
  #3  
Old December 26th, 2005, 04:45 AM
BobR
Guest
 
Posts: n/a
Default Re: vector 'push_back' problem


Al wrote in message
<1135542026.524551.101160@o13g2000cwo.googlegroups .com>...[color=blue]
>What is the problem with this code? It always crashes at the
>'push_back'. I found that thanks to debugging. Any Ideas?[/color]

// 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()

[color=blue]
>Other question: what is a faster way to convert a string to an integer?
>Is there a built-in function to do that?[/color]

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


  #4  
Old December 26th, 2005, 09:15 AM
Jim Langston
Guest
 
Posts: n/a
Default Re: vector 'push_back' problem

"BobR" <RemoveBadBobR@worldnet.att.net> wrote in message
news:UkKrf.353737$zb5.290058@bgtnsc04-news.ops.worldnet.att.net...[color=blue][color=green]
>>Other question: what is a faster way to convert a string to an integer?
>>Is there a built-in function to do that?[/color]
>
> 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);[/color]

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.


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles