Anfangsbuchstaben von 5 Worten sortieren

  • Themenstarter Themenstarter FuRi0uS
  • Beginndatum Beginndatum
F

FuRi0uS

Hi!

Ich habe mich bei einer Firma beworben und muss nun einen Aufnahmetest machen,
jedoch sind wir in der Schule erst bei if Verzweigungen, Arrays, usw.

Einer hier im Forum hat geschrieben dass man mit

std::map<std::string, std::map<std::string,int,std::greater<std::string> > >

die Aufgabe lösen könnte. Jedoch weiß ich nicht was std ist :confused: Bitte um eine kurze Erklärung..

Mein geschriebenes Programm bis jetz:

Code:
#include "iostream"
#include "string"
#include "conio.h"
using namespace std;
    

int main()
{
    char w;
    int taste;
    int exit=0;
    while(!exit)
    {
                stdm
                 cout << "Bitte 5 Vornamen eingeben: \n";
                 string namen[5];
                 for(int i=1;i<=5;i++)
                 {
                        cin >> namen[i-1];
                 }
                 
                 for(int j=1;j<=5;j++)
                 {
                        cout << j << ".) " << namen[j-1] << "\n";
                 }
                 cout << "Zum beenden druecken Sie eine die Taste 'e'.";
                 if(kbhit()) 
                 {
                             taste=getch();
                 }
                 sort(namen,namen+sizeof(namen));
                 switch(taste)
                 {
                              case 101: exit++;
                 }
    }
}

bitte um einen konkreten vorschlag/lösungsansatz, da es sehr wichtig ist!
 
was ich vergessen hab: ich darf KEIN bubblesort, quicksort, etc. verwenden!

es muss eine einfache lösung geben wie z.b.

#include "iostream.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"

void main()
{
cout << "Bitte 5 Vornamen eingeben: \n";
string namen[5];
for(int i=1;i<=5;i++)
{
cin >> namen[i-1];
}
Array.Sort(namen);
foreach (int v in namen)
Console.WriteLine("Element: {0}", v);

}


nur leider funktioniert diese NICHT :(
 
Ka ob es geht:
C++:
#include <iostream> // #include "iostream"
#include <string> // #include "string"
#include <algorithm> // std::sort, std::copy
#include <iterator> // std::ostream_iterator
#include <cctype> // std::tolower

int main()
{
    char exit(0);
    while (exit != 'j')
    {
        std::cout << "Bitte geben Sie fünf Vornamen ein!\n";
                 
        std::string names[5];
                 
        for (std::size_t i(0); i < 5; ++i)
        { 
            std::cout << i + 1 << ".) ";
            std::getline(names[i]);
        }

        std::clog << "Sortieren ...\n";
        std::sort(namen, namen + 5);
        std::cout << "Ausgabe: \n";
        std::copy(names, names + 5, std::ostream_iterator<std::string>(std::cout, "\n"));

        std::cout << "Beenden? (j / n)";
        std::cin >> exit;
        exit = std::tolower(exit);
    }
}
...

Im Notfall mach es so:
C++:
#include <iostream> // #include "iostream"
#include <string> // #include "string"
#include <algorithm> // std::sort, std::copy
#include <iterator> // std::ostream_iterator
#include <cctype> // std::tolower

const bool compare_first_letter(std::string const& lhs, std::string const& rhs) 
{ return (*lhs.begin()) < (*rhs.begin()); }

int main()
{
    char exit(0);
	while (exit != 'j')
    {
		std::cout << "Bitte geben Sie fünf Vornamen ein!\n";
                 
		std::string names[5];
                 
		for (std::size_t i(0); i < 5; ++i)
		{ 
			std::cout << i + 1 << ".) ";
			std::getline(names[i]);
		}

		std::clog << "Sortieren ...\n";
		std::sort(namen, namen + 5, compare_first_letter);
		std::cout << "Ausgabe: \n";
		std::copy(names, names + 5, std::ostream_iterator<std::string>(std::cout, "\n"));

		std::cout << "Beenden? (j / n)";
		std::cin >> exit;
		exit = std::tolower(exit);
    }
}
... das sollte wohl gehen ...
 
Zuletzt bearbeitet:
Zurück