在这个例子中,QT 中的 foreach 循环不起作用

foreach loop in QT not working in this example?

本文关键字:中的 QT foreach 循环 不起作用      更新时间:2023-10-16

我通常不会陷入循环。但看起来我对循环逻辑一无所知。当我在内部 for 循环中放置一个中断时,我得到的值始终为 0。但是,当我删除中断时,我得到了正确的值。我的代码出了什么问题?

void ContentCache::getAggregatorList()
{
    TLOG_FUNC_ENTER();
    ContentAggregator  *aggregator =  ContentAggregator::Instance();
    QList <ContentID>   aggContentList;
    /* Get the service list from the aggregator */
    aggregator->getContentList(CONTENT_TYPE_LIVE_SERVICE, aggContentList);
    QList<Attribute> attributeListOut;
    ContentID content;
    foreach( content, aggContentList )
    {
        TLOG_INFO("SJ..main loop");
        //if(content.source == TERRESTRIAL_BROADCAST_SOURCE )
        //  {
        doGetServiceAttributes(content,attributeListOut);
        unsigned int attributeIndex = 0;
        foreach( Attribute attr, attributeListOut)
        {
            TLOG_INFO("SJ..Inner loop");
            if(attr.name == "chanNum")
            {
                TLOG_INFO("SJ..The  attr channelNum is = "<<attr.value.toInt());
                if( attr.value.toUInt() == 999)
                {
                    TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
                    attr.name = "chanNum";
                    attr.value = 200;
                    attributeListOut.replace(attributeIndex,attr);
                    TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
                    m_cacheDB->terrestrialUpdateService(content,attributeListOut);
                }
                else
                {
                    TLOG_INFO("SJ..The unmodified attr channelNum is"<<attr.value.toUInt());
                }
                break;
            }
            attributeIndex++;
            //  }
        }
    }
    // getAttributeValues(ContentID id, QList<QString> value, QList<Attribute> &attributeListOut);
}*

没有休息时间,你正在计算所有Attribute s。 休息后,您只会将属性计算到第一个具有attr.name == "chanNum"属性,这似乎不太可能有用。 目前还不完全清楚你打算让break做什么。

帮助解决这些事情的一种方法是只提取逻辑并以简化的形式尝试它。 例如(以 C++11 形式):

#include <iostream>
#include <string>
int main()
{
    std::string string1{"BONGO"};
    std::string string2{"This is string2"};
    std::cout << "With the breakn";
    for ( auto ch : string1 )
    {
        unsigned int attributeIndex = 0;
        for( auto c: string2 )
        {
            if( c > 'a')
            {
                if( c > 'i')
                {
                    std::cout << ch << c << "+";
                }
                else
                {
                    std::cout << ch << c << "-";
                }
                break;
            }
            std::cout << ch << c << "@";
            attributeIndex++;
        }
        std::cout << attributeIndex << 'n';
    }
    std::cout << "Without the breakn";
    for ( auto ch : string1 )
    {
        unsigned int attributeIndex = 0;
        for( auto c: string2 )
        {
            if( c > 'a')
            {
                if( c > 'i')
                {
                    std::cout << ch << c << "+";
                }
                else
                {
                    std::cout << ch << c << "-";
                }
                // no break
            }
            std::cout << ch << c << "@";
            attributeIndex++;
        }
        std::cout << attributeIndex << 'n';
    }
}

这将生成以下输出:

With the break
BT@Bh-1
OT@Oh-1
NT@Nh-1
GT@Gh-1
OT@Oh-1
Without the break
BT@Bh-Bh@Bi-Bi@Bs+Bs@B @Bi-Bi@Bs+Bs@B @Bs+Bs@Bt+Bt@Br+Br@Bi-Bi@Bn+Bn@Bg-Bg@B2@15
OT@Oh-Oh@Oi-Oi@Os+Os@O @Oi-Oi@Os+Os@O @Os+Os@Ot+Ot@Or+Or@Oi-Oi@On+On@Og-Og@O2@15
NT@Nh-Nh@Ni-Ni@Ns+Ns@N @Ni-Ni@Ns+Ns@N @Ns+Ns@Nt+Nt@Nr+Nr@Ni-Ni@Nn+Nn@Ng-Ng@N2@15
GT@Gh-Gh@Gi-Gi@Gs+Gs@G @Gi-Gi@Gs+Gs@G @Gs+Gs@Gt+Gt@Gr+Gr@Gi-Gi@Gn+Gn@Gg-Gg@G2@15
OT@Oh-Oh@Oi-Oi@Os+Os@O @Oi-Oi@Os+Os@O @Os+Os@Ot+Ot@Or+Or@Oi-Oi@On+On@Og-Og@O2@15

使用这样的技术可以帮助您抽象地制定逻辑,然后在实际项目中自信地进行编码。

我错过了清理对象 - 在压力下。这是我愚蠢问题的愚蠢解决方案。QList attributeListOut没有被覆盖,事实上它被附加到这里。这就是问题所在。理想情况下不应该是这种情况。但是,这完全取决于操作员如何超载。

void ContentCache::getAggregatorList()
{
    TLOG_FUNC_ENTER();
    ContentAggregator  *aggregator =  ContentAggregator::Instance();
    QList <ContentID>   aggContentList;
    /* Get the service list from the aggregator */
    aggregator->getContentList(CONTENT_TYPE_LIVE_SERVICE, aggContentList);
    QList<Attribute> attributeListOut;
    ContentID content;
    foreach( content, aggContentList )
    {
        TLOG_INFO("SJ..main loop");
        //if(content.source == TERRESTRIAL_BROADCAST_SOURCE )
        //  {
        attributeListOut.clear();
        doGetServiceAttributes(content,attributeListOut);
        unsigned int attributeIndex = 0;
        foreach( Attribute attr, attributeListOut)
        {
            TLOG_INFO("SJ..Inner loop");
            if(attr.name == "chanNum")
            {
                TLOG_INFO("SJ..The  attr channelNum is = "<<attr.value.toInt());
                if( attr.value.toUInt() == 999)
                {
                    TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
                    attr.name = "chanNum";
                    attr.value = 200;
                    attributeListOut.replace(attributeIndex,attr);
                    TLOG_INFO("SJ..The changed attr channelNum is"<<attr.value.toUInt());
                    m_cacheDB->terrestrialUpdateService(content,attributeListOut);
                }
                else
                {
                    TLOG_INFO("SJ..The unmodified attr channelNum is"<<attr.value.toUInt());
                }
                break;
            }
            attributeIndex++;
            //  }
        }
    }
    // getAttributeValues(ContentID id, QList<QString> value, QList<Attribute> &attributeListOut);
}*