如何在C ++循环中使用storedlist [英] How to use storedlist in a loop in C++

查看:94
本文介绍了如何在C ++循环中使用storedlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在循环中使用storedList,对于具有两个值的brandList,如(Id,text)我希望将其存储在结果中,我试过这种方式,我不知道该怎么做。伙计们,请帮助我。



How to use storedList in a loop, for brandList having two values like (Id, text) and I want to store this in the result, I tried this way, I don't know what to do. Please help me, guys.

String *Util::Brands( ) __gc[]
{
    String *result __gc[];
    result = 0;
    SortedList  *brandList = 0;

    if (brandList == 0)
    {
        brandList = BrandCodes();            
    }       

    for (int i =0; i<= brandList->Count; i++)
    {
        result = new String * __gc[2];
        //result[0] = brandList[i]
        //result[1] = brandList[i]
    }

    return result;
}





我的尝试:





What I have tried:

for (int i =0; i<= brandList->Count; i++)
    {
        result = new String * __gc[2];
        //result[0] = brandList[i]
        //result[1] = brandList[i]
    }

推荐答案

这可能不是解决方案,因为它不清楚,你想要完成什么,请提供一个例子如果brandList包含a,b,c,那么结果应该看起来像x,y ,z



但是已经有几个明显的问题,也许这会给你一些提示如何继续:



您的索引 i 超出了最后一个循环中brandList数组的范围。修复:使用< 代替< =

This might not be a solution since it is not clear, what you want to accomplish, please provide an example of "If brandList contains a,b,c, then result should look like x,y,z"

But there are several obvious issues already, maybe this gives you a few hints how to proceed:

Your index i is exceeding the bounds of the brandList array in the last loop. Fix: use < instead of <=:
for (int i =0; i < brandList->Count; i++)
    {
        result = new String * __gc[2];
        //result[0] = brandList[i]
        //result[1] = brandList[i]
    }



另一个问题是在每个循环中你丢弃了之前的结果:


Another problem is that in every loop you discard the previous result:

for (int i =0; i < brandList->Count; i++)
    {
        result = new String * __gc[2]; // This throws away the previous content
                                       // of result and allocates a new one.
        result[0] = brandList[i];
        result[1] = brandList[i];
    }
    // now result[0] and [1] contains brandList[brandList.Count-1] only


这篇关于如何在C ++循环中使用storedlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆