我一般都不会陷入循环中。但看起来我对循环逻辑一无所知。当我在内部for循环中放置一个中断时,我得到的值总是为0.但是,当我删除break时,我得到了正确的值。我的代码出了什么问题?
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);
}*
答案 0 :(得分:0)
没有休息,你计算所有Attribute
。通过中断,您只计算属性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 break\n";
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 break\n";
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
使用这样的技术可以帮助您抽象地编写逻辑,然后对实际项目充满信心地进行编码。
答案 1 :(得分:0)
我错过了清理对象 - 在压力下。这是我愚蠢问题的愚蠢解决方案。 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);
}*