【问题标题】:C++: How to see if an element in an array is over a certain value?C ++:如何查看数组中的元素是否超过某个值?
【发布时间】:2020-11-15 01:41:07
【问题描述】:

C++ 新手,所以我提前为我的无能道歉。

我需要在函数 Main 之外编写一个函数来检查数组的元素(由用户输入)以查看它们是否超过 10。如果是,我还需要打印该数量的元素作为元素本身的列表。如果没有,我需要显示一条消息说没有。

我尝试了各种方法,但这是我不断重复的方法。你能帮忙吗?

void print_over_ten(const int list[], int length)
    //list[] contains user entered elements
    //length contains the length of the array, list[]
{
    int index=0;   //counter
    int amount;  //number of elements over 10
    int number_over_ten;    //variable for numbers over ten

    cout << "\nThese numbers are over ten: ";

    if (index > 10 && index <= length)
    {
        number_over_ten = index-1;
        cout << number_over_ten << " ";
        index++;
    }
    else
        cout << "\nThere are no numbers over 10.\n";

    amount = index;

    cout << "There are " << amount << " numbers over 10.\n";    
}

我认为大部分内容可能是错误的,因此请随意丢弃。

非常感谢任何帮助。谢谢!

【问题讨论】:

  • 你需要一个循环。 for( int i=0; i &lt; length; ++i) { // Do something with list[i] }

标签: c++ arrays function


【解决方案1】:

如果只需要统计匹配的元素,可以使用简单的循环,例如:

void print_over_ten(const int list[], int length)
{
    int amount = 0;
    for (int i = 0; i < length; ++i) 
    {
        if (list[i] > 10)
            ++amount;
    }
    if (amount > 0)
        cout << "There are " << amount << " numbers over 10.";
    else
        cout << "There are no numbers over 10.";
} 

或者,标准的std::count_if()算法,例如:

#include <algorithm>

void print_over_ten(const int list[], int length)
{
    size_t amount = std::count_if(list, list+length, [](int i){ return i > 10; });
    if (amount > 0)
        cout << "There are " << amount << " numbers over 10.";
    else
        cout << "There are no numbers over 10.";
} 

但是,如果您需要实际显示匹配的单个数字,则需要单独比较每个数字,例如:

void print_over_ten(const int list[], int length)
{
    for(int i = 0; i < length; ++i)
    {
        if (list[i] > 10)
        {
            int amount = 1;
            cout << "These numbers are over ten: " << list[i];
            for (++i; i < length; ++i)
            {
                if (list[i] > 10)
                {
                    ++amount;
                    cout << " " << list[i];
                }
            }
            cout << "\nThere are " << amount << " numbers over 10.";
            return;
        }
    }

    cout << "There are no numbers over 10.";
} 

或者,您可以将匹配的数字收集到一个容器中,例如 std::vector,例如:

#include <vector>

void print_over_ten(const int list[], int length)
{
    std::vector<int> numbers;
    for (int i = 0; i < length; ++i) 
    {
        if (list[i] > 10)
            numbers.push_back(list[i]);
    }
    if (!numbers.empty())
    {
        cout << "These numbers are over ten:";
        for(int i : numbers) {
            cout << " " << i;
        }
        cout << "\nThere are " << numbers.size() << " numbers over 10.";
    }
    else
        cout << "There are no numbers over 10.";
} 

【讨论】:

    【解决方案2】:

    您的if 条件基本正确,但您需要在使用循环迭代数组时进行检查:

    for (int i = 0; i < length; ++i)
    {
      if (list[i] > 10)
        ++index;
    }
    
    if (index > 10)
        cout << "There are " << index << " numbers over 10.\n";    
    else
        cout << "\nThere are no numbers over 10.\n";
    

    关于命名的小提示:变量index 没有描述它的作用。您可能应该将其命名为 counter,就像您在评论中所写的那样,或 amount,因为您稍后将其分配给具有该名称的变量。


    比循环更好的是使用这样的算法:

    int amount = std::count_if(list, list + length, [](int i) { return i > 10; });
    

    【讨论】:

      【解决方案3】:

      您可以使用&lt;algorithm&gt; STL 文件中的std::copy_if 大大简化此过程:

      std::vector<int> get_over_10(const int list[], int length) {
        std::vector<int> over_10;
        std::copy_if(list, list+length, std::back_inserter(over_10),
            [](int i) { return i > 10; });
        return over_10;
      }
      

      【讨论】:

      • i &lt;= 10 应该是 i &gt; 10
      猜你喜欢
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 2010-09-22
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多