1

i have a long string variable and i want to search in it for specific words and limit text according to thoses words.

Say i have the following text :

"This amazing new wearable audio solution features a working speaker embedded into the front of the shirt and can play music or sound effects appropriate for any situation. It's just like starring in your own movie"

and the words : "solution" , "movie".

I want to substract from the big string (like google in results page):

"...new wearable audio solution features a working speaker embedded..." and "...just like starring in your own movie"

for that i'm using the code :

for (std::vector<string>::iterator it = words.begin(); it != words.end(); ++it)
{
    int loc1 = (int)desc.find( *it, 0 );    
    if( loc1 != string::npos )
    {
        while(desc.at(loc1-i) && i<=80)
        {       
            i++;
            from=loc1-i;
            if(i==80) fromdots=true;
        }
        i=0;
        while(desc.at(loc1+(int)(*it).size()+i) && i<=80)
        {
            i++;
            to=loc1+(int)(*it).size()+i;
            if(i==80) todots=true;
        }

        for(int i=from;i<=to;i++)
        {    
            if(fromdots) mini+="...";
            mini+=desc.at(i);
            if(todots) mini+="...";    
        }    
      }

but desc.at(loc1-i) causes OutOfRange exception... I don't know how to check if that position exists without causing an exception !

Help please!

7
  • 3
    What's with the insane level of indentation? Please make your code easier to read, so that people don't have to work so hard to help you. Apr 12, 2010 at 4:18
  • You mean you want to remove from the text any of the given strings? Apr 12, 2010 at 4:19
  • Oh i'm sorry, i spent time formating it but apparrently it's not clear.. i'm working on it sorry ;)
    – Youssef
    Apr 12, 2010 at 4:20
  • He wants to remove all sections of text that are more that N words away from any occurrences of the given strings. Apr 12, 2010 at 4:21
  • @yox: I meant your indentation within the code, just like Marcelo commented on. I'll delete my comment as it's a little redundant. Apr 12, 2010 at 4:21

3 Answers 3

1

This is an excellent exercise in taking advantage of what the STL has to offer. You simply open a reference and cherry-pick algorithms and classes for your solution!

#include <iostream> // algorithm,string,list,cctype,functional,boost/assign.hpp
using namespace std;

struct remove_from {
    remove_from(string& text) : text(text) { }
    void operator()(const string& str) {
        typedef string::iterator striter;
        striter e(search(text.begin(), text.end(), str.begin(), str.end()));
        while( e != text.end() ) {
            striter b = e;
            advance(e, str.length());
            e = find_if(e, text.end(), not1(ptr_fun<int,int>(isspace)));
            text.erase(b, e);
            e = search(text.begin(), text.end(), str.begin(), str.end());
        }
    }
private:
    string& text;
};

int main(int argc, char* argv[])
{
    list<string> toremove = boost::assign::list_of("solution")("movie");
    string text("This amazing new wearable ...");
    for_each(toremove.begin(), toremove.end(), remove_from(text));
    cout << text << endl;
    return 0;
}
1

You can just check desc.size() - if it's less than the index you're looking up + 1 then you'll get an exception

0

The problem is that you start iterating at the first word, then try and check the word before it, hence the OutOfRange Exception.

Your first if could be:

if( loc1 != string::npos && loc1 != 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.