【问题标题】:#include <cmath> stopped working in C++20?#include <cmath> 在 C++20 中停止工作?
【发布时间】:2021-07-15 12:41:29
【问题描述】:

总结问题: 我的目标是生成一个限制的奇数,将它们存储在一个向量中,然后输出它们的平方。

描述您的尝试: 到目前为止,我在 int main() 之前的开头使用了 #include ,然后我使用了几个 if 语句来检查限制是否为零,如果是则输出错误消息。如果限制> 0,那么我使用 pow(num,2) 对奇数进行平方,但是,这不起作用并给出错误的答案。例如,它给出 13 的平方为 600ish,这显然是错误的。请给建议。我的完整代码在这里,它很简单,所以我没有放太多的cmets:

#include<iostream>
#include<format>
#include<cmath>
#include<vector>
using namespace std;


 int main()
 {  
int limit{};
cout << "Enter a limit of odd integers: \n";
cin >> limit;

vector<int>oddnumb(limit);

if (limit == 0)
{
    cout << "You MUST enter a number>0, then restart the program. \n";
    return 0;
}

if (limit > 0)
{
    cout << "Odd numbers are as follows: \n";
    for (size_t i{}; i < limit-1; ++i)
    {
        oddnumb[i] += ++i;

        cout << oddnumb[i] << endl;
        
    }
    cout << "Squared odd numbers follow: \n";
    for (size_t i{}; i < limit - 1; ++i)
    {
        oddnumb[i] += ++i;

        cout << pow(oddnumb[i],2) << endl;

    }

}

【问题讨论】:

  • 提示:(13 +13) ^2 == 676
  • fwiw,将pow 与整数一起使用总是错误的。参见例如:stackoverflow.com/questions/25678481/….
  • 让我给你一个建议:休息一下。您的代码和问题看起来您已经为此付出了很多努力,但不幸的是,所有这些都朝着错误的方向发展。这与 C++20 中有关 cmath 的更改无关。您的代码中的某些部分只能被理解为尝试修复某些东西。休息后,你应该从头开始。用奇数元素填充向量,并确保向量实际上具有您期望的元素。打印矢量很简单:for (const auto&amp; e : v) std::cout &lt;&lt; v &lt;&lt; " ";
  • @Electrical_engineer_student 您可以使用@后跟名称来提及/回复。
  • 由于您使用的是 C++20,因此您可以使用 &lt;ranges&gt; 执行以下操作:auto sep = ""; for (auto n : numbers | filter(is_odd) | transform(square)) { cout &lt;&lt; sep &lt;&lt; n; sep = " ";} cout &lt;&lt; "\n"; 您必须自己创建 is_oddsquare,这不是很难...auto is_odd = [](int n) { return 1 == n % 2; };auto square = [](int n) { return n * n; };

标签: c++ for-loop c++20


【解决方案1】:

我已经编写了您想要做的工作版本。我敦促您先自己弄清楚,这是最好的学习方式,然后根据我在这里获得的版本检查您的版本。请注意,这当然不是最好的方法。但我写的方式应该很容易理解。在编程中,有很多方法可以解决任何问题,最好的方法可以说是……可行的方法!祝您在 C++ 之旅中好运!

#include <cmath>
#include <iostream>
#include <vector>
int main()
{
    double input{};
    while (input < 1)//validate that input is greater than 0 here by looping if it isn't
    {
        std::cout << "Enter a limit of odd integers: ";
        std::cin >> input;
        if (input < 1)//if a valid range is entered this is never shown
        {
            std::cout << "Enter a valid range. Greater than 0. \n\n";
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
    }

    std::vector<double> oddNumbers{};
    std::cout << "Odd numbers:\n";
         //notice we're starting this loop at 1, since 0 is even we just avoid it
    for (double i = 1.0; i <= input; i += 2.0) //you can add more than just 1 to i
    {
        oddNumbers.push_back(i);// here we're putting the value of i at the back of the vector
        std::cout << i << " squared is " << pow(i, 2.0) << '\n';
         //we could avoid using a vector by printing the squared value of i within this loop
    }

    std::cout << "Squared Odd Numbers:\n";
         //we do need to start this loop at 0 to access the first element of the vector
    for (unsigned int i = 0; i < oddNumbers.size(); i++)
         //we also increase i by just one each time to access each element we stored
    {
        std::cout << pow(oddNumbers[i], 2.0) << '\n';
    }
    return 0;
}

【讨论】:

  • P.S.不要觉得有必要支持或接受这个答案,我认为那些有 cmets 的家伙帮助你指出了正确的方向。我只是想提供这个,以防你陷入困境和沮丧。我自己对 C++ 也很陌生,所以这样的事情对我来说很有趣。
  • 您好,感谢您的回答。我自己试过了,得到了正确的答案。让我知道您对此的看法:onecompiler.com/cpp/3x5qwnvgy
  • @Z With Glasses ok 这样一个编译器链接不起作用,因为它不支持 #include 所以你可能想使用visual studio std latest(C++20) 来运行它.
  • @Electrical_engineer_student 有用吗?这是最重要的部分。有一些很好遵循的最佳实践,并且匹配与您合作的任何人的格式总是一个好主意。除此之外,您始终可以尝试使代码更高效,使其更易于阅读,随时发表评论并在需要时进行重构。如果你有兴趣,这里有一个很好的风格指南。 google.github.io/styleguide/cppguide.html
  • 哦,我应该补充一下,我还没有在我的编译器中使用 c++20 的预览功能,所以我不能用 std::format 编译它,但逻辑对我来说看起来不错。干得好!
猜你喜欢
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 2013-08-29
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多