如果有数组,则在数组中查找副本并将其显示为C ++

时间:2013-10-30 02:57:14

标签: c++ arrays function duplicates

我的程序从输入文件中获取值并将它们传递给名为gpa的数组。我创建了一个函数,在gap数组中找到最小的值并显示它,但是如果有的话,我还需要显示一个重复的值。我该怎么做呢。

提前谢谢

#include <iostream>
#include <fstream>

using namespace std;

void lowestGpa(string names[], double gpa[], int SIZE){
    int count;
    double lowest = gpa[0];
    string name;

    for (count = 0; count < SIZE; count++)
    {
        if (gpa[count] <= lowest)
        {
            lowest = gpa[count];
            name = names[count];
        }
    }

    cout << name << " " << lowest;
}

int main()
{
    ifstream infile;
    infile.open("GPA.txt");

    int const SIZE = 15;
    string names[SIZE];
    double gpa[SIZE];

    while (infile)
    {
        for(int i = 0; i < SIZE; i++)
        {
            infile >> names[i] >> gpa[i];
        }
    }

    lowestGpa(names, gpa, SIZE);

    infile.close();

    return 0;
}

5 个答案:

答案 0 :(得分:1)

//Add a loop to cout the lowest's name 
for (count = 0; count < SIZE; count++)
{

    if (gpa[count] == lowest )

    {
        cout << names[count]<< " " << lowest;
    }

}

但这种方法并不完美,有2个循环。

答案 1 :(得分:1)

最简单(但不一定是计算最快)的方式可能是对数据进行排序。然后,您可以找到最低值,因为它将位于数组中的位置0,您可以通过迭代列表并查找score [n] ==得分[n + 1]的实例来检查重复项。

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    const size_t size = 7;
    int array[size] = { 70, 20, 60, 40, 10, 30, 40 };
    // note: the second argument is just past the last element,
    // it's where the array has *ended*
    std::sort(&array[0], &array[size]);

    for (size_t i = 0; i < size; ++i)
        std::cout << array[i] << " ";
    std::cout << endl;

    // lowest:
    std::cout << "Lowest: " << array[0] << std::endl;

    // duplicate check
    // skip 1 and use backwards-looking checks, this saves us having
    // to do math in the loop condition.
    for (size_t i = 1; i < size; ++i) {
        if (array[i] == array[i - 1])
            std::cout << (i - 1) << " and " << i <<
                " contain " << array[i] << std::endl;
    }
    return 0;
}

现场演示:http://ideone.com/3WKrHR

答案 2 :(得分:1)

无需对整个数组进行排序,也无需循环两次。只需使用额外的向量来存储GPA最低的名称。

void lowestGpa(string names[], double gpa[], int SIZE){
    double lowest = gpa[0];
    vector<int> lowids(1, 0);

    for (int count = 0; count < SIZE; count++)
    {
        if (gpa[count] < lowest)
        {
            lowest = gpa[count];
            lowids.clear();
            lowids.push_back(count);
        }
        else if (gpa[count] == lowest)
        {
            lowids.push_back(count);
        }
    }

    for (vector<int>::const_iterator i = lowids.begin(); i != lowids.end(); ++i)        
        cout << names[*i] << " " << lowest << endl;
}

答案 3 :(得分:0)

您可能希望找到所有最低GPA的重复项。

#include <algorithm>
#include <numeric_limits>

void lowestGpa(string names[], double gpa[], int size){
    double lowest = numeric_limits<double>::infinity(); //or 1. / 0
    for(int i = 0; i < size; i++) {
        lowest = min(lowest, gpa[i]);
    }

    for(int i = 0; i < size; i++) {
        if(gpa[i] == lowest) {
            cout << names[i] << " " << gpa[i];
        }
    }
}

这在O(n)

中运行

此外,size0时,这不会崩溃。

答案 4 :(得分:0)

您可以使用std::setstd::set_differencestd::sort查找最低重复元素:

#include <vector>
#include <set>
#include <iostream>

int main() {
    int gpas[] = {100,30,97,67,89,100,90,67,100,67,30,1,1};//sample values

    std::vector<int> vec(gpas, gpas + sizeof(gpas)/sizeof(int));//this constructor is only valid for stack allocated arrays
    std::set<int> cpy(vec.begin(), vec.end());//create a set with the unique gpas in a set
    std::vector<int> duplicates;

    std::set<int>::const_iterator it;

    std::sort(vec.begin(), vec.end());//sort the set as set_difference requires that the container is sorted

    //find the duplicates using set_difference. This will find all duplicates
    //(which will be repeated n - 1 times in `duplicates` for n > 1)
    std::set_difference(vec.begin(), vec.end(), cpy.begin(), cpy.end(), std::back_inserter(duplicates));

    //this creates a set from all the duplicates found
    std::set<int> unique_duplicates(duplicates.begin(), duplicates.end());

    //output the duplicates
    for (it = unique_duplicates.begin(); it != unique_duplicates.end(); ++it) {
        std::cout << *it << std::endl;
    }

    //if you want the lowest value of the duplicates:
    //you can use the min_element from <algorithm>:
    //std::min_element(unique_duplicates.begin(), unique_duplicates.end());

    //However, as the vector `vec` was already sorted, this ensures that
    //all the other results obtained from the sorted vector will ALREADY
    //be sorted. Hence, if size is > 0, then we can simply look at the
    //first element alone.
    if (unique_duplicates.size() > 0) {
        it = unique_duplicates.begin();
        std::cout << "The LOWEST duplicate element is: " << *it << std::endl;
    }

    return 0;
}

从发布的样本数据中,输出:

1
30
67
100

作为重复元素

而且,1是最低的重复元素。

参考文献:

http://www.cplusplus.com/reference/algorithm/set_difference/