Skip to main content
added 458 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

Reading###Reading a file:

Why###Why are you doing this:

You###You seem to reversing the line just to print it:

for(std::vector<std::string>::const_iterator& loop = sVec.rbegin(); loop != sVec.rend(); ++ loop)
{                               
    cout << (*loop) << " is a Palindrome " <<endl;                                  
}                                                                                           

###Note on efficiency:

Using push_back() on an vector can be in-effecient if you are pushing a lot of data. As evertime the vector runs out of space it must allocate more memory and copy the string to the new buffer. You help by pre-allocting space.

sVec.reserve(1000); // Guess at the size
                    // Note when the vector runs out of space it will approximately
                    // double its internal buffer.

Reading a file:

Why are you doing this:

You seem to reversing the line just to print it:

for(std::vector<std::string>::const_iterator& loop = sVec.rbegin(); loop != sVec.rend(); ++ loop)
{                               
    cout << (*loop) << " is a Palindrome " <<endl;                                  
}                                                                                           

###Reading a file:

###Why are you doing this:

###You seem to reversing the line just to print it:

for(std::vector<std::string>::const_iterator& loop = sVec.rbegin(); loop != sVec.rend(); ++ loop)
{                               
    cout << (*loop) << " is a Palindrome " <<endl;                                  
}                                                                                           

###Note on efficiency:

Using push_back() on an vector can be in-effecient if you are pushing a lot of data. As evertime the vector runs out of space it must allocate more memory and copy the string to the new buffer. You help by pre-allocting space.

sVec.reserve(1000); // Guess at the size
                    // Note when the vector runs out of space it will approximately
                    // double its internal buffer.
added 1022 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

Why are you doing this:

    else{
        getline(fin, str);
    }

Mixing operator >> and getline() makes the code hard to read.
Either use all operator >> or use getline() to get a line at a time then parse the line internally.

Note:: operator>> will ignore the '\n' at the end of the line and work. So if your file is a list of words one per line there is no need to use getline(). If on the other hand your file is a lot of text and you only want the first word on each line then you need to use getline().

###Method 1: Use operator>>

 // If your input file has one word per line (or you want to use all words)
 while(file >> word)
 {
      // Do stuff
 }

###Method 1: Use getline()

 // If your input file contains lots of words
 // But you only want to use the first word on each line
 while(std::getline(file, line))
 {
     std::stringstream  linestream(line);
     linestream >> word;

     // Do stuff
 }

You seem to reversing the line just to print it:

reverse(sVec.begin(), sVec.end());                          
for(int i =0; i < sVec.size(); i++){                                
    cout << sVec[i] << " is a Palindrome " <<endl;                                  
}                                                                                           

Rather than reversing the array, just iterate it in reverse order:

for(std::vector<std::string>::const_iterator& loop = sVec.rbegin(); loop != sVec.rend(); ++ loop)
{                               
    cout << (*loop) << " is a Palindrome " <<endl;                                  
}                                                                                           

Why are you doing this:

    else{
        getline(fin, str);
    }

Mixing operator >> and getline() makes the code hard to read.
Either use all operator >> or use getline() to get a line at a time then parse the line internally.

Note:: operator>> will ignore the '\n' at the end of the line and work. So if your file is a list of words one per line there is no need to use getline(). If on the other hand your file is a lot of text and you only want the first word on each line then you need to use getline().

###Method 1: Use operator>>

 // If your input file has one word per line (or you want to use all words)
 while(file >> word)
 {
      // Do stuff
 }

###Method 1: Use getline()

 // If your input file contains lots of words
 // But you only want to use the first word on each line
 while(std::getline(file, line))
 {
     std::stringstream  linestream(line);
     linestream >> word;

     // Do stuff
 }

You seem to reversing the line just to print it:

reverse(sVec.begin(), sVec.end());                          
for(int i =0; i < sVec.size(); i++){                                
    cout << sVec[i] << " is a Palindrome " <<endl;                                  
}                                                                                           

Rather than reversing the array, just iterate it in reverse order:

for(std::vector<std::string>::const_iterator& loop = sVec.rbegin(); loop != sVec.rend(); ++ loop)
{                               
    cout << (*loop) << " is a Palindrome " <<endl;                                  
}                                                                                           
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

Reading a file:

while(fin){ 

    fin >> str; 
    sWords.push_back(str); 
    if(!fin){ 
        break; 
    } 

You push the value str before checking it worked. It should be:

while(fin)
{ 
    fin >> str; 
    if(!fin){ 
        break; 
    } 
    sWords.push_back(str); 

Or even better would be:

while(fin >> str)
{    
    sWords.push_back(str);