【问题标题】:Print new line ONLY at the end of a vector - "if" statement outside of a "for" loop仅在向量末尾打印新行 - “for”循环外的“if”语句
【发布时间】:2017-09-22 14:42:41
【问题描述】:

代码:

#include <iostream>
#include <vector>

int main () {

std::cout << "Please, enter the number of iterations: ";    //Inputs

int iterations;
std::cin >> iterations;

std::cout << "Which term would you like to know: ";

int term;
std::cin >> term;   //End inputs

std::vector<int> series;     //definition of vector "series"

for (int n = 1;  n <= iterations; n++) {    //creation of "series"

    int next = (2 * n - 3);
    series.push_back(next);

}

std::cout << "The value of term "   //prints the n term
          << term
          << " is: "
          << series[term-1]         //term-1 "adjust" the indexing
          << std::endl;

std::cout << "The entire serie up to "
          << iterations
          << " terms is: ";

for (int i = 0;  i < series.size(); i++) {      //prints (elements of vector) "series"

    std::cout << series[i] << ' ';

    if (i == series.size()-1) {     //new line only at the end the series

        std::cout << std::endl; 
    }

}

return 0;

}

我得到了 9/10 的评论:“如果循环内的条件只会满足一次,但每次都会检查。移出循环”。 我真的不知道如何将 if 语句置于循环之外。 该 for-if 语句的范围是仅在向量“系列”的末尾添加一个新行。 我无法考虑其他任何事情,但可以肯定的是我有足够的经验并且还有另一个更优雅的解决方案。 我在这里问是因为我必须提交另一个作业,我不想提交同样的错误。

PS:另一条评论是:轻度过度评论。我真的评论太多了吗?

【问题讨论】:

  • 你根本不需要那个if。例如return 0“仅在系列结束时”发生。
  • if 块仅在迭代的 end 时检查,唯一的进入条件是您是否正在为 last 元素提供服务.这在语义上与 not 完全具有 if 块相同 - 并将内容移动到循环之后立即(例如,在 for 之后无条件调用 std::cout &lt;&lt; std::endl 而不是有条件地检查最后一个元素每次迭代。)
  • 是的,您正在添加毫无意义的 cmets,例如:std::vector&lt;int&gt; series; //definition of vector "series" 对于任何编写 C++ 代码的人来说都是显而易见的
  • 要添加到评论内容...您还可以编写自我评论代码...所以要创建您的系列或打印您的系列,您可以创建名为 seriesCreationprintSeries 的函数, 或initializeSeries 这将有助于一些毫无意义的 cmets 并且仍然解释你在做什么。具有有意义变量名称的可读代码比明显的 cmets 要好得多。

标签: c++ loops for-loop if-statement vector


【解决方案1】:

你在for loop的末尾打印endl,所以你可以在退出循环时添加endl的行并删除for循环处的行,它会给你同样的结果

代码:

for (int n = 1;  n <= iterations; n++) {    //creation of "series"

    int next = (2 * n - 3);
    series.push_back(next);

}

std::cout << "The value of term "   //prints the n term
          << term
          << " is: "
          << series[term-1]         //term-1 "adjust" the indexing
          << std::endl;

std::cout << "The entire serie up to "
          << iterations
          << " terms is: ";

for (int i = 0;  i < series.size(); i++) {      //prints (elements of vector) "series"

    std::cout << series[i] << ' ';
}
std::cout << std::endl; 
return 0;

}

【讨论】:

    【解决方案2】:

    您要做的是在最后一个元素之后打印一个换行符。在这里,您正在检查每个元素是否是最后一个元素。但是,您不需要这样做。

    “在最后一个元素之后”的另一种方法是“在所有元素之后”,所以在循环之后。只需使用 for 循环处理所有元素,然后打印换行符。

    我没有给出代码,因为它是一个作业,但这应该足够清楚。

    至于评论:不要记录显而易见的事情。当然,对于不同的层次,显然是有所不同的。但是对于“系列”向量的声明,例如,注释不会在代码中添加任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2021-12-24
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多