简体   繁体   English

C++ 打印带有 cout 的结构

[英]C++ print a structure with cout

I have been trying to find a simple way to return 2 values from a function and found online that creating a structure to store the values was the easiest method of doing so.我一直在尝试找到一种简单的方法来从 function 中返回 2 个值,并在网上发现创建一个结构来存储这些值是最简单的方法。 Now I have written the structure and the function I can not figure out how to actually print the structure.现在我已经编写了结构和 function 我无法弄清楚如何实际打印结构。 I have seen many other posts about this online, but I don't understand the answers to them.我在网上看到了很多关于这个的帖子,但我不明白他们的答案。 Could anybody please explain to me how I can fix my code without using any technical terms (without explanation).任何人都可以向我解释如何在不使用任何技术术语(无需解释)的情况下修复我的代码。 Preferably an addition/modification of my current code with some research material on the subject.最好用一些关于该主题的研究材料来添加/修改我当前的代码。 I am not experienced in coding at all, nor do I have an education centered around coding.我根本没有编码经验,也没有以编码为中心的教育。 I am just doing this in my spare time for a bit of fun.我只是在业余时间做这件事以获得一点乐趣。

#include <iostream>
#include <cmath>

struct values {
    float value1;
    float value2;
};

values quadratic(int a, int b, int c) {
    float d = sqrt(b*b - 4*a*c);
    float x1 = (-b + d)/2*a;
    float x2 = (-b - d)/2*a;
    values result = {x1, x2};
    return result;
};

int main() {
    std::cout << quadratic(1, 2, -1);
};

One common way to do this is to overload operator<< .一种常见的方法是重载operator<< See this question for further information.有关更多信息,请参阅此问题

struct values {
    friend std::ostream& operator<<(std::ostream&, const values&);

    float value1;
    float value2;
};

std::ostream& operator<<(std::ostream& os, const values& v) {
  return os << v.value1 << ", " << v.value2;
}

There are at least three ways to do this.至少有三种方法可以做到这一点。

Method 1: Get the struct result and print it方法一:获取struct结果并打印

This is the simplest solution.这是最简单的解决方案。

int main() {
    values vs = quadratic(1, 2, -1);
    std::cout << vs.value1 << ", " << vs.value2 << "\n";
}

Method 2: Use automatic structured bindings方法 2:使用自动结构化绑定

This requires C++17 minimum (I think, you shouldn't be using anything less anyway):这至少需要 C++17 (我认为,无论如何你都不应该使用更少的东西):

int main() {
    auto [a, b] = quadratic(1, 2, -1);
    std::cout << a << ", " << b << "\n";
}

It's prettier than Method 1, but basically the same thing.它比方法 1 更漂亮,但基本相同。

Method 3: Overload the stream insertion operator方法三:重载stream插入算子

This is where you question was marked as a duplicate and linked to do do exactly this:这是您的问题被标记为重复并链接到这样做的地方:

std::ostream & operator << (std::ostream & outs, const values & vs) {
    return outs << vs.value1 << ", " << vs.value2;
}

int main() {
  std::cout << quadratic(1, 2, -1) << "\n";
}

This is perhaps the prettiest at the call site, but does require your values struct to be convertible into a string (which is what the overloaded << operator does).这可能是调用站点上最漂亮的,但确实需要将您的values结构转换为字符串(这就是重载的<<运算符所做的)。

Method 4: Return a tuple instead of a custom struct方法 4:返回元组而不是自定义结构

This is basically a rehash of 2.这基本上是 2 的重新散列。

#include <cmath>
#include <iostream>
#include <tuple>

std::tuple <float, float> quadratic(int a, int b, int c) {
    float d = sqrt(b*b - 4*a*c);
    float x1 = (-b + d)/2*a;
    float x2 = (-b - d)/2*a;
    return std::make_tuple(x1, x2);
};

int main() {
    std::cout << quadratic(1, 2, -1) < "\n";
};

That's all I'm gonna bother with off the top of my head, but I think it covers all the bases.这就是我要打扰的所有内容,但我认为它涵盖了所有基础。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM