2014-04-07 2 views
-2

나는 일반적으로 루핑에 빠지지 않습니다. 그러나 루핑 논리에서는 여기에 우둔한 것처럼 보입니다. inner for 루프에 중단을 넣을 때 얻을 수있는 값은 항상 0입니다. 그러나 break를 제거하면 올바른 값을 얻습니다. 내 코드에서 무엇이 잘못 되었습니까?QT의 foreach 루프가이 예제에서 작동하지 않습니까?

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

네트워킹을 만드십니까? 그렇다면, 어쩌면 당신은 속보를하는 동안 약간의 응답을 놓칠 수 있습니까? – vahancho

+0

당신이 추적하고있는 변수입니다 (즉 :'break'와 "right values"를 0으로 줄 것입니다)? – fritzone

+0

attr.value.toInt() –

답변

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); 
}* 
0

휴식 시간없이 모두 Attribute입니다. 휴식과 함께, 당신은 단지 유용 할 것 같지 않은 attr.name == "chanNum"을 가진 첫 번째 속성까지 계산합니다. break이 의도 한 바가 완전히 명확하지 않습니다.

이러한 일을 돕는 한 가지 방법은 단순한 논리로 논리를 추출하고 단순화 된 형식으로 시도하는 것입니다. (11 형태 ++ C에서) 예를 들면 :

#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 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
Without the break 
[email protected]@[email protected][email protected] @[email protected][email protected] @[email protected][email protected][email protected]@[email protected]@[email protected] 
[email protected]@[email protected][email protected] @[email protected][email protected] @[email protected][email protected][email protected]@[email protected]@[email protected] 
[email protected]@[email protected][email protected] @[email protected][email protected] @[email protected][email protected][email protected]@[email protected]@[email protected] 
[email protected]@[email protected][email protected] @[email protected][email protected] @[email protected][email protected][email protected]@[email protected]@[email protected] 
[email protected]@[email protected][email protected] @[email protected][email protected] @[email protected][email protected][email protected]@[email protected]@[email protected] 

사용 기술은 당신이에 대한 자신감과 함께 추상적으로 다음 코드 논리를 작업 할 수 있도록 할 수 있습니다 진짜 프로젝트.

관련 문제