【问题标题】:Is there a way to simplify this C++ integer loop array?有没有办法简化这个 C++ 整数循环数组?
【发布时间】:2015-08-13 00:44:15
【问题描述】:

这是我一直在学习用 C++ 制作的数组。有什么方法可以让我进一步简化吗?


cout

    int value[5] = { 17, 34, 51, 68, 85 }; // Creates an integer array list that contain a series of 5 values

    for (int loop = 0; loop < 5; loop++) {
        cout << "Integer value " << loop << ": " << value[loop] << endl;
    }

    cout << endl << "Array of double values" << endl; // First endl creates a blank line
    cout << "=======================" << endl;

    double numbers[10] = { 12.1, 24.2, 36.3, 48.4, 60.5, 72.6, 84.7, 96.8, 212.9, 3.0 }; // Creates an double integer array list that contains a series of 10 values

    for (int loop = 0; loop < 10; loop++) { // Loops array
        // Adding example " numbers[loop] = 77; " would set every number to 77

        cout << "Element at index " << loop << ": " << numbers[loop] << endl;
    }

    cout << endl << "Initializing with zero values" << endl; // First endl creates a blank line
    cout << "=======================" << endl;

    for (int loop = 0; loop < 1; loop++) { // Loops array
        // Adding " numbers[loop] = 77; " would set every number to 77
        cout << "Element at index " << loop << ": " << numbers[loop] << endl;
    }

    cout << endl << "Initializing with strings" << endl; // First endl creates a blank line
    cout << "=======================" << endl;

    // Array of strings
    string text = ( "Chair", "Table", "Plate" ) ;

    ;       for (int loop = 0; loop < 3; loop++) { // Loops array
        cout << "Element at index " << loop << ": " << text[loop] << endl;

        return 0;

【问题讨论】:

  • 不确定你想做什么
  • 你可以删除int numberArray[8] = {}; 它没有任何用处。没有真正进行计算,那么简化是什么意思?
  • 基于范围的 for 循环?
  • 通过简化,我的意思是如何进一步压缩代码。我只是在制作一个基本的整数数组。我删除了 numberArray。
  • 您可以编写一个打印数组的函数。这就是你要找的东西吗?

标签: c++ arrays loops integer


【解决方案1】:

你永远不会使用你在这里声明的数组:

int numberArray[8] = {};

这整个部分看起来像是从上面重复的:

for (int loop = 0; loop < 10; loop++) { // Loops array
// Adding " numbers[loop] = 77; " would set every number to 77
cout << "Element at index " << loop << ": " << numbers[loop] << endl;

就“简化”这一点而言,如果您使用相同数据类型的数组,您可以编写一个函数来接受数组、元素的数量,并在数组中循环输出您想要的任何内容。然后你可以每次都从你的main函数调用这个函数,而不是重复for循环

【讨论】:

  • 我更改了代码,在其中添加了一个字符串数组。但是,它最后没有显示文本数组。有任何想法吗?我修改了 OP 以显示代码。
  • string text = ( "Chair", "Table", "Plate" ) ; 不是有效的数组初始化(或字符串初始化!)。您可能一直在尝试的是 string text[] = {"Chair", "Table", "Plate"}; 这应该可以修复您的代码(并且您还缺少一个右括号 } 来关闭您的最后一个 for 循环)。
【解决方案2】:

也许你可以试试这样的:

template <typename T>
void Printer (const vector<T> &t_elementsToPrint)
{

    // For simple value printing
    for (auto value : t_elementsToPrint)
        cout << value << "\n";

    // If you want to know the index number
    for (auto it = begin(t_elementsToPrint); it != end(t_elementsToPrint); ++it)
    {
        cout << "Element at index " << distance(begin(t_elementsToPrint), it) << " : " << *it << "\n";
    }
}

int main (int argc, char* argv[])
{

    vector<double> numbers = { 12.1, 24.2, 36.3, 48.4, 60.5, 72.6, 84.7, 96.8, 212.9, 3.0 }; // Creates an double integer array list that contains a series of 10 values

    Printer<double>(numbers);

    vector<int> numbers2 = { 17, 34, 51, 68, 85 };

    Printer<int>(numbers2);

    return 0;
}

您可以将容器从向量更改为数组或其他可能适合您的容器,除非您确实需要原始访问权限,如果您正在寻找简单性,我建议您坚持使用 STL 容器。

【讨论】:

    猜你喜欢
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多