打乱2D字符串数组



我正在做一个C++程序,在这个程序中我想打乱一个数组(或数组的一部分(。这是阵列:

        string colorTheme[8][8] = {
            {"blue", "blue", "green", "green", "violet", "violet", "teal", "teal"},
            {"beige", "beige", "red", "red", "indigo", "indigo", "pink", "pink"},
            {"cyan", "cyan", "yellow", "yellow", "orange", "orange", "azure", "azure"},
            {"purple", "purple", "lime", "lime", "tangerine", "tangerine", "fuschia", "fuschia"},
            {"brown", "brown", "gray", "gray", "black", "black", "white", "white"},
            {"olive", "olive", "crimson", "crimson", "silver", "silver", "gold", "gold"},
            {"maroon", "maroon", "coral", "coral", "plum", "plum", "ivory", "ivory"},
            {"aqua", "aqua", "jade", "jade", "amber", "amber", "ruby", "ruby"}
        };

如果我想打乱前n行和n列,我该怎么做?理想情况下,我会运行

shuffle(n);

因为colorTheme与CCD_ 1在同一类中。

你不能洗牌const数组,但你可以通过改变它来做到,我将发布一个洗牌2d数组的例子,如果你想:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <string>
#include <algorithm>
#include <iterator>
int main()
{
    // the hard, inefficient way
    {
        enum { N = 7, M = 13 } ;
        char dest[N][M] = { "zero", "one", "two", "three", "four", "five", "six" } ;
        std::srand( std::time(nullptr) ) ;
        for( int i = N-1 ; i > 0 ; --i ) // fisher yates shuffle
        {
            const int pos = std::rand() % (i+1) ;
            char temp[M] ;
            std::strcpy( temp, dest[pos] ) ;
            std::strcpy( dest[pos], dest[i] ) ;
            std::strcpy( dest[i], temp ) ;
        }
        for( const char* cstr : dest ) std::cout << cstr << ' ' ;
        std::cout << 'n' ;
    }
    // the simple, efficient way
    {
        enum { N = 7 } ;
        std::string dest[N] = { "zero", "one", "two", "three", "four", "five", "six" } ;
        std::srand( std::time(nullptr) ) ; // if it has not already been done
        std::random_shuffle( std::begin(dest), std::end(dest) ) ;
        for( const std::string& str : dest ) std::cout << str << ' ' ;
        std::cout << 'n' ;
    }
}

解决此问题的最佳方法是将2D数组转换为1D,因为对1D数组进行混洗要简单得多。在掩护下,创建一个int阵列搅乱器;用0-n^2的值填充int数组,并使用类似rand的方法对其进行混洗。从那里,使用int数组中的值作为字符串数组的新位置。打乱字符串数组后,将其转换回1D数组。这是我创建的一个简单的c++源文件(可以随意使用(。

#include <iostream>
#include <string>
using namespace std;
void shufflePos(int n, int arr[]);
void printArray(int *a, int length) {
    cout << "[ ";
    for (int i = 0; i < length; i ++) {
        cout << a[i];
        if (i != length - 1) {
            cout << ", ";
        }
    }
    cout << " ]n";
}
void shufflePos(int n, int arr[]) {
    for(int i = 0; i < n; i++) {
        arr[i] = i;
    } 
    // shuffle positions
    srand(time(0));
    for(int i = 0; i < (n - 2); i++) {
        /*if(arr[i] != i) // i has already been swapped
            continue;
        */
        int tmp = arr[i];
//        cout << "i = " << i << ", n - i = " << (n - i) << ", ";
        int random = rand();
//        cout << "random = " << random << ", ";
        int nextPos = i + random % (n - i);
//        cout << "nextPosition = " << nextPos << endl;
        arr[i] = arr[nextPos]; // swap
        arr[nextPos] = tmp;
    }
        //printArray(arr, n);
/*    bool chck = check(arr, n);
    if(chck == false)
        cout << "FALSE" << endl;
    else
        cout << "TRUE" << endl; */
}
void swapString(string arr[], int pos1, int pos2) {
    string tmp = arr[pos1];
    arr[pos1] = arr[pos2];
    arr[pos2] = tmp;
    return;
}
void shuffleString(string strs[], int len) {
    int valArr[len];
    shufflePos(len, valArr);
    string to[len];
    // copying values into another array
    for(int i = 0; i < len; i++) {
        to[i] = strs[valArr[i]];
    }
    // copying to[] into strs[]
    for(int i = 0; i < len; i++) {
        strs[i] = to[i];
    }
}
int main() {
    string colorTheme[8][8] = {
        {"blue", "blue", "green", "green", "violet", "violet", "teal", "teal"},
        {"beige", "beige", "red", "red", "indigo", "indigo", "pink", "pink"},
        {"cyan", "cyan", "yellow", "yellow", "orange", "orange", "azure", "azure"},
        {"purple", "purple", "lime", "lime", "tangerine", "tangerine", "fuschia", "fuschia"},
        {"brown", "brown", "gray", "gray", "black", "black", "white", "white"},
        {"olive", "olive", "crimson", "crimson", "silver", "silver", "gold", "gold"},
        {"maroon", "maroon", "coral", "coral", "plum", "plum", "ivory", "ivory"},
        {"aqua", "aqua", "jade", "jade", "amber", "amber", "ruby", "ruby"}
    };
    cout << "What size of array do you want?" << endl;
    int i;
    cin >> i;
    int n = i * i; // length of 1D array
    string darr[n]; // 1D array
    for(int r = 0; r < i; r++) { // fill values of 1D array // rows
        for(int c = 0; c < i; c++) { // columns
            darr[(i * r + c)] = colorTheme[c][r];
        }
    }
    cout << 1 << endl;
    shuffleString(darr, n);
    cout << 2 << endl;
    // convert 1D array back into 2D array
    for(int r = 0; r < i; r++) {
        for(int c = 0; c < i; c++) {
            colorTheme[c][r] = darr[(i * r) + c];
        }
    }
    for(int r = 0; r < i; r++) { // rows
        for(int c = 0; c < i; c++) { // columns
            cout << ": " << colorTheme[c][r] << " ";
        }
        cout << endl;
    }
}

相关内容

  • 没有找到相关文章

最新更新