我不知道如何创建一个以数字/字母顺序打印出我的整数或字符串的程序

I dont know how to create a program that prints out my integers or strings in numerical/alphabetical order

本文关键字:我的 打印 顺序 整数 程序 字符串 何创建 创建 数字 一个以 我不知道      更新时间:2023-10-16

我不知道如何制作这本书的练习,希望我以数字/字母顺序打印出三个整数或三个字符串。

我已经尝试使用if语句来解决此问题,但它只是失败了,因为我是初学者。

使用这样的代码,如果

cout << a << b << c << endl;

预期结果是我输入但按数字顺序打印出来的数字。

#include <iostream> 
#include <string> 
using namespace std; 
int main() { 
    cout << "Enter three whole numbers" << endl; int a,b,c; cin >> a >> b >> c; 
    if(a<b<c) { 
        cout << a << "," << b << "," << c << endl; 
    } 
    if(b<a<c) { 
        cout << b << "," << a << "," << c << endl; 
    } 
    if(c<a<b) { 
        cout << c << "," << a << "," << b << endl; 
    } 
    return 0; 
}
#include <iostream>
#include <utility>
int main() { 
    std::cout << "Enter three whole numbers" << std::endl; int a,b,c; std::cin >> a >> b >> c;
    if (a > b) std::swap(a, b); // a < b ? c
    if (b > c) std::swap(b, c); // a ? b < c and a < c
    if (a > b) std::swap(a, b); // a < b < c
    std::cout << a << "," << b << "," << c << std::endl;
    return 0; 
}