简体   繁体   English

在不使用迭代器的情况下,将向量中的整数相加,第一个和第二个元素的和为to,依此类推

[英]Add the sum of integers located in a vector without using iterators, the sum is to for the first and second element and so forth

#include <iostream>
#include <vector>

using namespace std;

int main() 
{
    int typedNos;
    if (cin >> typedNos) 
    {
        vector <int> inputNos{ typedNos }; 

        while (cin >> typedNos)
        {
            inputNos.push_back(typedNos); 
        }


        for (decltype (inputNos.size()) n = 1; n < inputNos.size(); ++n)
        {

            cout << inputNos[0] + inputNos[1] << '\t' << inputNos[(2 * n) - 1]
                   + inputNos[(2 * n)] << endl;

            return 0;
        }
    }
    else
    {
        cerr << " Wrong input type or no input was typed!" << endl;
        //return -1;
    }
}

Everything works fine till the output statement in the for loop is reached. 一切正常,直到到达for循环中的输出语句为止。 The first two pairs of the vector's elements are manually added to account for zero. 向量的前两对元素会手动添加为零。 The rest are to be added automatically. 其余的将自动添加。 But this only works for the first pair. 但这仅适用于第一对。

So, for example, an input of: 因此,例如,输入:

1 2 3 4 5. 1 2 3 4 5。

Will give you an output of: 将为您提供以下输出:

3 5. 3 5。

Instead of 3 5 7 9. 而不是3 5 7 9。

This is where I have an issue. 这是我遇到的问题。 I have seen other methods of solving this problem but my question is why the sequence 2n (even positions) and 2n-1 (odd positions) do not work for the entire vector? 我已经看到了解决此问题的其他方法,但我的问题是为什么序列2n(偶数位置)和2n-1(奇数位置)不适用于整个矢量? Remember this question does not allow me to use iterators. 请记住,这个问题不允许我使用迭代器。 Thanks. 谢谢。

The problem lies in your for-loop . 问题出在您的for-loop Using return inside a loop will still exit from the current function. 在循环内使用return仍将从当前函数退出。 Your current function is main , so this ends the program. 您当前的函数是main ,因此该程序结束。

I'm not really sure why you think you need 2 * n . 我不确定您为什么认为需要2 * n It seems you want to iterate over every object, not every second one. 似乎您要遍历每个对象,而不是第二个对象。

for (std::size_t n = 1; n < inputNos.size(); ++n) {
    std::cout << inputNos[n] + inputNos[n-1] << '\t';
}
std::cout << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用指针输入元素后,如何打印数组的第一个和最后一个元素、第二个和倒数第二个等的总和? - How to print the sum of the first and last element, second and second last and so on.., of an array after you input the elements using pointers? 整数向量的和 - Sum of a vector of integers 将向量中的整数序列求和 - Sum a sequence of integers in a vector 在第一个数字和第二个数字之间都可被4和6整除的所有整数的总和 - Sum of all integers that are both divisible by 4 and 6 between the first and second number 使用推力::reduce 计算 8 位整数向量的和而不会溢出 - Using thrust::reduce to compute the sum over a vector of 8 bit integers without overflow 整数的向量…的向量的向量之和 - Sum of vector of vectors of vectors … of integers 如何打印第一个和最后一个元素的总和,然后是第二个和倒数第二个的总和,依此类推? - How do I print the sum of first and last elements, followed by the sum of the second and second-to-last, and so on? 无法使用 for 循环对整数向量求和 c++ - Can't sum a vector of integers using for loop c++ 比较第一个向量中的不同向量迭代器和擦除元素 - Compare different vector iterators and erase element from the first vector 如何在不使用loop或std :: accumulate()的情况下计算向量的总和? - How to calculate the sum of a vector without using loop or std::accumulate()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM