将代码从数组移植到矢量

时间:2013-08-16 03:20:39

标签: c++ arrays vector char

我开发了一个简单的填字游戏生成器,重要的是我使用了一个数组作为网格。我现在正在做的是设置它,以便用户可以选择他们想要使用的网格大小。使用数组我不能这样做所以我想改变我的代码以使用向量。我遇到了这个困难,尝试了一些不同的东西,却无法让它发挥作用:

//    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&gt; array_convertto_vector(array 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] != '\0') { // 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] == '\0') {
                        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] != '\0') {
                        flag = false;
                    }
                }
                for (int x = 0; x < random_word.size(); x++) {
                    if (wordsearch[i+x][j] == random_word[x] || wordsearch[i+x][j] == '\0') {
                        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] == '\0') {
                        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] == '\0') {
                        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<<" ";
            }
        }
    }

}

如果我没有使代码足够清晰,你必须取消注释向量部分并注释掉我挑出来的三行中的数组部分以使其工作。

Theres是我用文字加载文件的一部分所以我不得不取消注释该部分。如果它仍然没有编译,因为它让我知道,我将重新发布一个有希望的工作版本的代码。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

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