如何在lambda中返回字符串而不是bool?

时间:2016-02-10 17:09:08

标签: c++

  

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

#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;
}

1 个答案:

答案 0 :(得分:2)

auto compare = [](int x, int y) {
  return x < y ? "x is less than y" : "x is greater than y";
};