将代码从数组移植到向量

Porting code from array to vector

本文关键字:向量 数组 代码      更新时间:2023-10-16

我开发了一个简单的填字游戏生成器,其中重要的一点是我使用了一个数组作为网格。我现在正在做的是设置它,这样用户就可以选择他们想要使用的网格大小。对于数组,我无法做到这一点,所以我想更改代码以使用向量。我对此有困难,尝试了一些不同的方法,但无法使其发挥作用:

//    std::vector<std::vector<char> > wordsearch;
//    std::vector<std::vector<char *> > wordsearch;
char wordsearch [10][11] = {0};

第三行是与代码一起正常工作的数组。第一行使程序崩溃,第二行运行良好,但抱怨"当我将字符附加到向量时,从char到char*的转换无效

我也试过

//    std::vector<std::vector<char> > wordsearch_vector(wordsearch, wordsearch +     sizeof(wordsearch) / sizeof(char));

但它也抱怨char-to-char*转换。在我的其他尝试中,我试图编写函数将数组转换为向量,但数组必须在其维度上进行数字定义,没有变量或未指定,供用户稍后定义,例如:std::vector>arrayconverto_vector(数组a);(数组未定义)

有什么建议吗?这是我的代码:

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <iterator>
using namespace std;
int center_text(string string_word/*add second parameter if setw will vary*/) {
    int spaces = 10 - string_word.size();
    return spaces / 2;
}
int main() {
    srand(time(NULL));
    const char* const a_to_z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
    int random_char;
//    std::vector<std::vector<char> > wordsearch;
//    std::vector<std::vector<char *> > wordsearch;
    char wordsearch [10][11] = {0};
    int random_choice;
    int f_or_b;
    int random_word_number;
    int temp_i;
    int temp_j;
    string random_word;
    bool flag;

    string words_array[] = {"CAT", "HELLO", "GOODBYE", "DOG", "BAT", "NEW", "SAY", "MAY", "DAY", "HAY", "CELLO", "ORANGES", "LINK", "ROBIN"};
    vector<string> words_vector (words_array, words_array + sizeof(words_array) / sizeof(string));
    string words_found_array[] = {};
    vector<string> words_found_vector (words_found_array, words_found_array + sizeof(words_found_array) / sizeof(string));
    //vector<string> words_vector;
    //vector<string> words_found_vector;
//    ifstream myfile("Words.txt");
//    copy(istream_iterator<string>(myfile), istream_iterator<string>(),
//         back_inserter(words_vector)); //MAKE SURE TO LOAD INTO VECTOR ONLY ONCE, NOT EACH TIME PROGRAM LOADS!!!
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 11; j++) {
            random_choice = rand() % 5;
            f_or_b = rand() % 2;
            random_word_number = -1;
            if (words_vector.size() != 0) {
                random_word_number = rand() % words_vector.size();
                random_word = words_vector[random_word_number];
            }
            if (j == 10) {
                wordsearch[i][j] = 'n';
            }
            else if (wordsearch[i][j] != '') { // prevents overwriting horizontal words, or add to j in else if loop instead of temp_j
                continue;
            }
            else if (random_choice == 1 && random_word.size() < 11-j && words_vector.size() != 0) { //or <= 10-j
                flag = false;
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i][j+x] == random_word[x] || wordsearch[i][j+x] == '') {
                        flag = true;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                temp_j = j;
                if (flag == true) {
                    if (f_or_b == 1) { //reverse string
                        reverse(random_word.begin(), random_word.end());
                    }
                    for (int x = 0; x < random_word.size(); x++) {
                        wordsearch[i][temp_j] = random_word[x];
                        temp_j += 1;
                    }
                if (f_or_b == 1) { //reverse back
                    reverse(random_word.begin(), random_word.end());
                }
                words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
                words_vector.erase(words_vector.begin()+random_word_number);
                }
                else {
                    int random_char = rand() % 26 + 0;
                    wordsearch[i][j] = a_to_z[random_char];
                }
            }
            else if (random_choice == 2 && random_word.size() <= 10-i && words_vector.size() != 0) {
                flag = false;
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i+x][j] != '') {
                        flag = false;
                    }
                }
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i+x][j] == random_word[x] || wordsearch[i+x][j] == '') {
                        flag = true;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                temp_i = i;
                if (flag == true) {
                    if (f_or_b == 1) {
                        reverse(random_word.begin(), random_word.end());
                    }
                    for (int x = 0; x < random_word.size(); x++) {
                        wordsearch[temp_i][j] = random_word[x];
                        temp_i += 1;
                }
                if (f_or_b == 1) {
                    reverse(random_word.begin(), random_word.end());
                }
                words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
                words_vector.erase(words_vector.begin()+random_word_number);
                }
                else {
                    int random_char = rand() % 26 + 0;
                    wordsearch[i][j] = a_to_z[random_char];
                }
            }
            else if (random_choice == 3 && random_word.size() <= 10-i && random_word.size() < 11-j && words_vector.size() != 0) { //or <= 10-j
                flag = false;
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i+x][j+x] == random_word[x] || wordsearch[i+x][j+x] == '') {
                        flag = true;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                temp_i = i;
                temp_j = j;
                if (flag == true) {
                    if (f_or_b == 1) {
                        reverse(random_word.begin(), random_word.end());
                    }
                    for (int x = 0; x < random_word.size(); x++) {
                        wordsearch[temp_i][temp_j] = random_word[x];
                        temp_i += 1;
                        temp_j += 1;
                    }
                    if (f_or_b == 1) {
                        reverse(random_word.begin(), random_word.end());
                    }
                  words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
                  words_vector.erase(words_vector.begin()+random_word_number);
                }
                else {
                    int random_char = rand() % 26 + 0;
                    wordsearch[i][j] = a_to_z[random_char];
                }
            }
            else if (random_choice == 4 && random_word.size() <= 10-i && random_word.size() > 11-j && words_vector.size() != 0) { //or >= 10-j
                flag = false;
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i+x][j-x] == random_word[x] || wordsearch[i+x][j-x] == '') {
                        flag = true;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                temp_i = i;
                temp_j = j;
                if (flag == true) {
                    for (int x = 0; x < random_word.size(); x++) {
                        wordsearch[temp_i][temp_j] = random_word[x];
                        temp_i += 1;
                        temp_j -= 1;
                }
                words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
                words_vector.erase(words_vector.begin()+random_word_number);
                }
                else {
                    int random_char = rand() % 26 + 0;
                    wordsearch[i][j] = a_to_z[random_char];
                }
            }
            else {
                    int random_char = rand() % 26 + 0;
                    wordsearch[i][j] = a_to_z[random_char];
            }
        }
    }
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 11; j++) {
                cout<<wordsearch[i][j];
        }
    }

//    std::vector<std::vector<char> > wordsearch_vector;
//    std::vector<std::vector<char> > wordsearch_vector(wordsearch, wordsearch + sizeof(wordsearch) / sizeof(char));
    random_shuffle(words_found_vector.begin(), words_found_vector.end());
    cout<<endl<<"Your words are:"<<endl;
    int counter = 0;
    int space_value;
    for (int x = 0; x < words_found_vector.size(); x++) {
        space_value = center_text(words_found_vector[x]);
        if (counter == 2) {
            for (int y = 0; y < space_value; y++) {
                cout<<" ";
            }
            cout<<words_found_vector[x]<<endl;
            counter = 0;
        }
        else {
            for (int y = 0; y < space_value; y++) {
                cout<<" ";
            }
            cout<<words_found_vector[x];
            counter += 1;
            for (int y = 0; y < space_value; y++) {
                cout<<" ";
            }
        }
    }
}

如果代码不够清晰,您必须取消对矢量部分的注释,并在我挑出的三行中注释掉数组部分,才能使其正常工作。

有一部分我在文件中加载了单词,所以我不得不取消对该部分的注释。如果它仍然没有编译,请告诉我,我会转发一个有望工作的代码版本。感谢您的帮助!

您需要初始化wordsearch向量,使其大小正确。否则,您的程序会尝试使用未分配的内存,(通常)会出现段错误。

可以使用上的第二个构造函数将向量初始化为特定大小http://en.cppreference.com/w/cpp/container/vector/vector.

请记住,您还需要初始化"内部"向量。

对于您的特定用例,正确的代码是:

std::vector<std::vector<char> > wordsearch(10,std::vector<char>(11));

请注意,您正在使用10个长度为11的矢量初始化wordsearch矢量。