选择与数组中第n个元素对应的字符串

时间:2013-03-21 09:13:23

标签: c++ arrays map

我在这里遇到一个非常烦人的问题。我在这个挑战中的目标是返回一个数组,按批量保存5个最活跃的买家的名字。 stockSymbols持有这些名称,而已购买的总数已成功计算并放入totalBought数组中。然后,我试图找出前5名最大的名字并将他们的名字放入totalSold并返回。

我是c ++的绝对新手,所以我知道这种做事方式是不正确的。它也不会编译,它会在整个地方抛出内存错误(从if(最大[0]< totalBought [i]))行开始或附近。

如果你们中的任何一个人知道更好的方法来使这项工作成功,我将非常感谢您的意见,或者如果您知道如何使这种方式起作用也非常有用。我真的很想找到最有效的方法(在速度方面)来计算它。

非常感谢。

string* Analyser::topFiveBuyers()
{
    // Your code
string* totalSold;
totalSold = new string[5];

    string stockSymbols[] = {"W Buffet", "P Lynch", "G Soros", "J Neff", "Hargreaves Lansdown",
        "Sippdeal", "Saga", "Halifax", "iWeb", "Alliance Trust", "Clubfinance", "Lloyds TSB", "Saxo" };

    int totalBought[13];

    for(int i = 0; i < nTransactions; i++)
    {
        for(int j = 0; j < 13; j++)
        {
            if(transArray[i].buyerName == stockSymbols[j])
            {
                totalBought[j] += transArray[i].numShares;
            }
        }
    }

    int biggest[] = {-1, -1, -1, -1, -1};

    for(int i = 0; i < 13; i++)
    {
        if(biggest[0] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = biggest[2];
            biggest[2] = biggest[1];
            biggest[1] = biggest[0];
            biggest[0] = totalBought[i];
            totalSold[0] = stockSymbols[i];
        }
        else if(biggest[1] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = biggest[2];
            biggest[2] = biggest[1];
            biggest[1] = totalBought[i];
            totalSold[1] = stockSymbols[i];
        }
        else if(biggest[2] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = biggest[2];
            biggest[2] = totalBought[i];
            totalSold[2] = stockSymbols[i];
        }
        else if(biggest[3] < totalBought[i])
        {
            biggest[4] = biggest[3];
            biggest[3] = totalBought[i];
            totalSold[3] = stockSymbols[i];
        }
        else if(biggest[4] < totalBought[i])
        {
            biggest[4] = totalBought[i];
            totalSold[4] = stockSymbols[i];
        }
    }

    return totalSold;
}

1 个答案:

答案 0 :(得分:0)

我建议使用组合地图和多地图来实现您想要的效果。我冒昧地将函数的结果更改为字符串的向量,它应该是诚实的:

std::vector<string> Analyzer::topFiveBuyers()
{
    typedef map<string, int> xact_map;
    xact_map xacts;
    for (int i=0;i<nTransactions;++i)
        xacts[transArray[i].buyerName] += transArray[i].numShares;

    // enumerate the map, beginning to end, throwing all elements
    //  into a multimap keyed by transaction count rather than name.
    //  note this considers an element "less" (and therefore at the
    //  beginning of the sort order) if the share count is *greater*.

    typedef multimap<int, string, std::greater<int>> highest_map;
    highest_map highest;
    for (xact_map::const_iterator it=xacts.begin(); it != xacts.end(); ++it)
        highest.insert(highest_map::value_type(it->second, it->first));

    // now just peel off the top five elements from the multimap.
    vector<string> results;
    int n=0;
    for (highest_map::const_iterator it=highest.begin(); 
         it != highest.end() && n<5; ++it,++n)
    {
        results.push_back(it->second);
    }

    return results;
}