[英]How to sort 3 strings variables in alphabetical order with c++?
I have 3 strings variables that user enter three words 我有3个字符串变量,用户输入三个单词
and must the program sort them alphabetically what I tried to make it with if statement but the problem is if the user insert the first 5 chars are the same I am tired of repeating the code for every time . 并且该程序必须按字母顺序对它们进行排序(如果我试图用if语句进行排序),但问题是,如果用户插入的前5个字符是相同的,我会厌倦每次重复的代码。
Here is my code : 这是我的代码:
#include <iostream>
using namespace std;
int main ()
{
string w1,w2,w3;
cout<<"please enter three words"<<endl;
cin >> w1 >> w2 >> w3;
if(w1[0] == w2[0])
{
if(w1[1] < w2[1] && w1[0] < w3[0]){
cout << w1;
if(w2[0] < w3[0])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[1] < w1[1] && w2[0] < w3[0]){
cout << w2;
if(w1[0] < w3[0])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[0] < w1[0] && w3[0] < w2[0]){
cout << w3;
if(w1[1] < w2[1])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w2[0] == w3[0])
{
if(w1[0] < w2[0] && w1[0] < w3[0]){
cout << w1;
if(w2[1] < w3[1])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[0] < w1[0] && w2[1] < w3[1]){
cout << w2;
if(w1[0] < w3[0])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[0] < w1[0] && w3[1] < w2[1]){
cout << w3;
if(w1[0] < w2[0])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w3[0] == w1[0])
{
if(w1[0] < w2[0] && w1[1] < w3[1]){
cout << w1;
if(w2[0] < w3[0])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[0] < w1[0] && w2[0] < w3[0]){
cout << w2;
if(w1[1] < w3[1])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[1] < w1[1] && w3[0] < w2[0]){
cout << w3;
if(w1[0] < w2[0])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w1[0] == w2[0] && w2[0] == w3[0])
{
if(w1[1] < w2[1] && w1[1] < w3[1]){
cout << w1;
if(w2[1] < w3[1])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[1] < w1[1] && w2[1] < w3[1]){
cout << w2;
if(w1[1] < w3[1])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[1] < w1[1] && w3[1] < w2[1]){
cout << w3;
if(w1[1] < w2[1])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else if(w1[0] == w2[0] && w2[0] == w3[0] && w1[1] == w2[1] && w2[1] == w3[1])
{
if(w1[2] < w2[2] && w1[2] < w3[2]){
cout << w1;
if(w2[2] < w3[2])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[2] < w1[2] && w2[2] < w3[2]){
cout << w2;
if(w1[2] < w3[2])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[2] < w1[2] && w3[2] < w2[2]){
cout << w3;
if(w1[2] < w2[2])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
else
{
if(w1[0] < w2[0] && w1[0] < w3[0]){
cout << w1;
if(w2[0] < w3[0])
{
cout << "\t" << w2 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w2;
}
}else if(w2[0] < w1[0] && w2[0] < w3[0]){
cout << w2;
if(w1[0] < w3[0])
{
cout << "\t" << w1 <<"\t" << w3;
}
else
{
cout << "\t" << w3<<"\t" << w1;
}
}else if(w3[0] < w1[0] && w3[0] < w2[0]){
cout << w3;
if(w1[0] < w2[0])
{
cout << "\t" << w1 <<"\t" << w2;
}
else
{
cout << "\t" << w2<<"\t" << w1;
}
}
}
//cout << w1[0] << "\t" << "\t" <<w2[0] << "\t" << w3[0];
return 0;
}
If there a way to make this with for loop but with these libraries only 如果有一种方法可以通过for循环但仅使用这些库
#include <iostream>
#include <stdio.h>
#include <string>
#include <algorithm>
I do not understand what your program tries to do. 我不了解您的程序尝试执行的操作。 But take into account that class
std::string
has relational operators. 但是要考虑到
std::string
类具有关系运算符。 So there is no need to compare each character individually. 因此,无需单独比较每个字符。
I would write the program the following way:) You may change the program such a way that it would ask the user to enter three strings. 我将以以下方式编写程序:)您可以更改程序,要求用户输入三个字符串。
#include <iostream>
#include <string>
#include <functional>
#include <utility>
int main()
{
std::string s1 = "tom", s2 = "fox", s3 = "hello";
auto r1 = std::ref( s1 ), r2 = std::ref( s2 ), r3 = std::ref( s3 );
if ( r2.get() < r1.get() ) std::swap( r1, r2 );
if ( r3.get() < r2.get() ) std::swap( r2, r3 );
if ( r2.get() < r1.get() ) std::swap( r1, r2 );
std::cout << s1<< '\t' << s2 << '\t' << s3 << std::endl;
std::cout << r1.get() << '\t' << r2.get() << '\t' << r3.get() << std::endl;
return 0;
}
The output is 输出是
tom fox hello
fox hello tom
Or you can use a straightforward approach 或者您可以使用简单的方法
#include <iostream>
#include <string>
int main()
{
std::string s1 = "tom", s2 = "fox", s3 = "hello";
if ( s1 <= s2 && s2 <= s3 )
{
std::cout << s1<< '\t' << s2 << '\t' << s3 << std::endl;
}
else if ( s1 <= s3 && s3 <= s2 )
{
std::cout << s1<< '\t' << s3 << '\t' << s2 << std::endl;
}
else if ( s2 <= s1 && s1 <= s3 )
{
std::cout << s2<< '\t' << s1 << '\t' << s3 << std::endl;
}
else if ( s2 <= s3 && s3 <= s1 )
{
std::cout << s2<< '\t' << s3 << '\t' << s1 << std::endl;
}
else if ( s3 <= s1 && s1 <= s2 )
{
std::cout << s3<< '\t' << s1 << '\t' << s2 << std::endl;
}
if ( s3 <= s2 && s2 <= s1 )
{
std::cout << s3<< '\t' << s2 << '\t' << s1 << std::endl;
}
return 0;
}
Try this: 尝试这个:
const unsigned int MAX_STRINGS = 3;
string string_container[MAX_STRINGS];
cout<<"please enter three words"<<endl;
cin >> string_container[0]
>> string_container[1]
>> string_container[2];
for (unsigned int i = 1; i < MAX_STRINGS; ++i)
{
if (string_container[i] <= string_container[i - 1])
{
std::swap(string_container[i - 1], string_container[i]);
i = 0;
}
}
It meets the requirement of having a for
loop. 它满足具有
for
循环的要求。
Edit 1: Using std::sort 编辑1:使用std :: sort
If you don't need a for
loop: 如果不需要
for
循环:
const unsigned int MAX_STRINGS = 3;
string string_container[MAX_STRINGS];
cout<<"please enter three words"<<endl;
cin >> string_container[0]
>> string_container[1]
>> string_container[2];
std::sort(&string_container[0], &string_container[MAX_STRINGS];
int main () {
string list[3];//Array of strings. Makes things easier.
cout<<"please enter three words"<<endl;
cin >> list[0] >> list[1] >> list[2];
//I might be using swap or compare wrong here
//but i guess it it more important that you know these functions exist.
if (list[0].compare(list[1]) < 0) swap(list[0], list[1]);
if (list[0].compare(list[2]) < 0) swap(list[0], list[2]);
if (list[1].compare(list[2]) < 0) swap(list[1], list[2]);
return 0;
}
you can also (and more preferably) use a for loop nested within a for loop as such: 您还可以(更优选地)使用嵌套在for循环中的for循环,如下所示:
int arraysize = 3;
for (int i = 0 ; i < arraysize ; i++) {
for (int j = i+1 ; j < arraysize) ; j++) {
if (list[i].compare(list[j]) < 0) swap(list[i], list[j]);
}
}
Try this (it's the code from Thomas Matthews, with a simple Bubble Sort ) 尝试一下(这是Thomas Matthews的代码,带有简单的Bubble Sort )
const unsigned int MAX_STRINGS = 3;
string string_container[MAX_STRINGS];
cout<<"please enter three words"<<endl;
cin >> string_container[0]
>> string_container[1]
>> string_container[2];
bool swaped;
do{
swaped = false;
for (unsigned int i = 1; i < MAX_STRINGS; ++i)
{
if (string_container[i] <= string_container[i - 1])
{
std::swap(string_container[i - 1], string_container[i]);
swaped = true;
}
}
}while(swaped);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.