【问题标题】:multiply and add to vector of integer乘并添加到整数向量
【发布时间】:2019-04-11 15:17:17
【问题描述】:

编写一个程序,提示用户将数据输入到整数向量中,并将第一个向量的值(通过乘以 8 并增加 100 修改)但以相反的顺序分配给另一个整数向量。 例子: 我们在第一个向量中输入值:

3   8   2   9   11  5  40 100 203 49

那么第二个向量的值是:

(49*8 +100) (203*8 +100) (100*8 +100) (40*8 +100) (5*8 +100) (11*8 +100) (9*8 +100)
(2*8 +100) (8*8 +100) (3*8 +100) .

所以这是被问到的问题,但到目前为止,我在代码中所能做的就是输入整数并将它们倒序,但我不知道如何将数学添加到编码中。(添加 100 和次8)

#include <iostream>
using namespace std;

int main(){
    int input[500], output[500], count, i;

    cout << "Enter number of elements in array\n";
    cin >> count;

    cout << "Enter " << count << " numbers \n";

    for(i = 0; i < count; i++){
        cin >> input[i];
    }

    // Copy numbers from inputArray to outputArray in 
    // reverse order 
    for(i = 0; i < count; i++){
        output[i] = input[count-i-1];
    }    

    cout << "Reversed Array\n";
    for(i = 0; i < count; i++){
        cout << output[i] << " ";
    }
    return 0;
}

【问题讨论】:

  • SImply output[i]=8*input[...] + 100. 检查 cout

标签: c++ arrays algorithm c++11 stdvector


【解决方案1】:

好吧,您没有在代码中的任何地方使用std::vector&lt;int&gt;,而是有两个大小为500 的普通数组。这不是要求。

阅读std::vector<> 并使用它们而不是那些数组,通过分配大小仅用户想要的(即相应地用户输入count)。

其次,您可以在复制/填充到output 向量数组时对input 向量中的每个元素进行计算。

这是一个示例代码:DEMO HERE

#include <iostream>
#include <vector>

int main()
{
    std::cout << "Enter number of elements in array\n";
    std::size_t count; std::cin >> count;

    std::cout << "Enter " << count << " numbers \n";
    std::vector<int> input(count, 0);
    // input element
    for (int& element : input) std::cin >> element;

    std::vector<int> output; output.reserve(count); // reserve memory for elements

    // apply to the output vector: for that    
    // simply reverse iterate input array using the while loop, 
    // emplace back the calculated result to the output array
    while (count--)
    {
        output.emplace_back((input[count] * 8) + 100);
    }

    // print out the output
    std::cout << "Reversed Array\n";
    for (const int element : output)    std::cout << element << " ";
    return 0;
}

输出

Enter number of elements in array
10
Enter 10 numbers
1 2 3 4 5 6 7 8 9 10
Reversed Array
180 172 164 156 148 140 132 124 116 108 

话虽如此,你也可以通过 std 算法函数 std::trasform 做到这一点。为此,iterate reversely std::transform 中的 input 向量并使用 lambda 函数(计算必要的),以及 insert back 到向量 output

SEE LIVE

#include <algorithm> // std::transform

auto doMath = [](int element) { return (element * 8) + 100; }; // lambda which does the calculation
std::transform(input.crbegin(), input.crend(), std::back_inserter(output), doMath);

或者您甚至可以通过在整个程序中只有一个向量数组来欺骗用户,您将在其中存储在进行计算后直接存储用户输入,如下所示:

SEE LIVE

#include <iostream>
#include <vector>

int main()
{
    std::size_t count; std::cin >> count;
    std::vector<int> vec; vec.reserve(count);
    // take the input and store the calculated value directly in the vec array
    while (count--)
    {
        int element; std::cin >> element;
        vec.emplace_back((element * 8) + 100);
    }
    return 0;
}

【讨论】:

  • 如何将此代码添加到我上面的代码中??
  • @AlexMesillas 实际上你给定的任务和你写的代码不匹配。您使用了一个不正常的数组 input[500], output[500],它必须使用 std::vector<>
  • 使用上面的代码,我试图运行它并查看它是如何工作的,但它只能让我最多输入 4 个数字而不是 10 个?
  • @AlexMesillas 第一个输入是计数。你会给它 4 个。这就是为什么你只能输入 4 个。为了区分输入,您可以提供调试消息。查看我更新的答案和链接。
【解决方案2】:

当你把 inputArray 中的数字逆序复制到 outputArray 时,你基本上是访问了 inputArray 的每一个元素,然后将它们放入新的数组中,所以你也可以在这一步中更改它们:

//take the element index (count - 1 - i) from inputArray, 
//multiply by 8, then increment by 100, 
//then assign output element index i equal to the result 

cout << "Reversed Array\n";
for(i = 0; i < count; i++){
    output[i] = ((input[count-i-1]) * 8) + 100;
}

【讨论】:

    【解决方案3】:

    实际计算是单行的:

    std::vector<int> input(count);
    // populate input vector here...
    std::vector<int> result(count);
    
    // one-liner, formatted for easier readability
    std::transform(std::cbegin(input), std::cend(input),
        std::rbegin(result),
        [](int incoming) { return 8 * incoming + 100; } );
    

    std::cbeginstd::cend 的调用分别返回指向input 的第一个元素和input 的末尾的迭代器。

    std::rbegin 的调用将reverse 迭代器返回到result;它从向量的末端返回到起点。

    最后一行是一个 lambda 函数——它使用名为 incomingint 参数调用并返回计算值。

    标准算法 std::transform 遍历输入范围 (std::cbegin(input) .. std::cend(input)),并且对于每个元素,应用 lambda 函数并将结果存储在输出范围 (std::rbegin(output);没有结束迭代器,因为算法在以下时间终止输入范围已用尽)。

    这看起来比其他答案中的一次性解决方案更复杂。但是一旦你习惯了使用迭代器和算法,这就会成为一种自然的公式,具有更高的清晰度和灵活性的优势。例如,如果您的要求发生了变化,并且您需要按顺序而不是倒序写入值,您只需将 std::rbegin(result) 更改为 std::begin(result)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-25
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多