如何在 lambda 中返回字符串而不是布尔值

How can I return string instead of bool in lambda?

本文关键字:布尔值 字符串 返回 lambda      更新时间:2023-10-16

在这种情况下,如何返回字符串"x 小于 y"或"x 大于 y"而不是布尔值?

#include <iostream>
using namespace std;
int main() {
    int n, m;
    cin >> n >> m;
    auto compare = [](int x, int y) { return  x < y; };
    cout << ((n == m) ? "the same" : to_string(compare(n, m)));
    return 0;
}
auto compare = [](int x, int y) {
  return x < y ? "x is less than y" : "x is greater than y";
};