简体   繁体   中英

How do I sort the letters in the array in C++?

I create a matrix. This matrix have random letters [az] and [AZ]

The harfGetir() function is create random letters. But I don't want the letters to repeat. How can i do it? And I want to sort letters.

Here is the code: What can I do?

const int M = 5;
const int N = 10;



static const char harfler[] = // a-z arası büyük ve küçük harfleri tanımladığımız değişken.
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int harfBoyut = sizeof(harfler) - 1; 


char harfGetir() {

return harfler[rand() % harfBoyut];

}

int main()
{

int A[M][N];

srand(time(NULL)); // değerleri yeniden atamak için programı baştan aldı.

for (int i = 0; i < M; i++) // satır döngüsü
    for (int j = 0; j < N; j++) // sütun döngüsü
    {
        A[i][j] = harfGetir(); // matris değerleri atama


    }

cout << setw(5) << "Harf Matrisi" << endl;

cout << endl;
for (int i = 0; i < M; i++) // satırdaki harfler
{

    for (int j = 0; j < N; j++) // sütundakı harfler

        cout << setw(4) << harfGetir();  // harfleri çeken kod

    cout << endl;

}
cout << endl;





system("pause");
return 0;
}

You can create a random permutation (use the random header, not the old srand ), and then get the n entries in the array.

Then you can sort them again.

For instance, if you have all the available letters in a string v :

std::random_device rd;
std::mt19937 g(rd());

std::shuffle(v.begin(), v.end(), g);

Then if you want 10 letters, you use the 10 first entries of v .

You can use std::shuffle() from <algorithm>

First of all,

#include <algorithm> // For shuffle
#include <random>    // For random-related things...

The harfler variable should be declared using std::string not the good ol' C-style const char*

// Stop using C-Style Strings already!!
static std::string harfler =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

and use it like this:

auto rng = std::default_random_engine{};
std::shuffle(harfler.begin(), harfler.end(), rng);

Example:

#include <iostream>
#include <algorithm>
#include <random>
#include <string>

static std::string harfler =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

auto CONFINE(int const x, std::string const& container, std::default_random_engine rng)
{
    if (x > signed(container.size() - 1))
    {
        std::shuffle(harfler.begin(), harfler.end(), rng);
        return x - signed(container.size() - 1);
    }
    return x;
}

int main()
{
    constexpr auto MAT_X_SIZE = 8;
    constexpr auto MAT_Y_SIZE = 8;
    auto rng = std::default_random_engine{};
    std::shuffle(harfler.begin(), harfler.end(), rng);
    std::vector<std::vector<int>> matrix(MAT_Y_SIZE, std::vector<int>(MAT_X_SIZE, 0));
    for (auto i = 0; i < MAT_Y_SIZE; i++)
        for (auto j = 0; j < MAT_X_SIZE; j++)
            matrix[i][j] = *std::next(harfler.begin(), CONFINE(MAT_Y_SIZE * i + j, harfler));
    for (auto elem : matrix)
    {
        for (auto elem2 : elem)
            std::cout << char(elem2) << ' ';
        std::cout << std::endl;
    }
    std::cin.get();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM