Best way to do pattern matching in QList



  • Hello, I need to match a pattern of about length 10, in a QList<qint8> of about size 50. All elements are either 0 or 1. The pattern may exist multiple times and I may need to find all of them. Any suggestions how to do this easily? I am not finding any QList compare functions.

    Currently I am doing the following, but not very convenient:

    @ for( int i=0; i<bits.size()-10; i++ )
    {
    if( bits.at(i)==0 && bits.at(i+1)==0 && bits.at(i+2)==0 && bits.at(i+3)==0
    && bits.at(i+4)==0 && bits.at(i+5)==0 && bits.at(i+6)==0 && bits.at(i+7)==0
    && bits.at(i+8)==1 && bits.at(i+9)==0 )
    ...
    @
    Thanks
    Ron



  • That's actually doing the job but if you want to save code or do it someway different, you could try:

    Create an empty QString

    Create a for to iterate trough all 10 values

    On every iteration, append the current value to you QString

    After the for ends, compare whatever pattern you want with your QString.

    @
    if (QString==QString)
    {
    // your code
    }
    @

    This may look longer to code, but it will allow you to compare what you have with any pattern, as well as they are the same size.



  • That is a good idea BlastDV. Maybe there is some easy way to compare a QList to a section of another QList.



  • I'm also interested to know. Let's see if some other idea pops up!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.