我开发了一个简单的字谜生成器,重点是我使用了一个数组作为网格。我现在正在做的是设置它,这样用户就可以选择他们想要使用的大小网格了。对于数组,我不能这样做,所以我想改变我的代码来处理向量。我在这方面有困难,尝试了一些不同的东西,但却无法让它发挥作用:
// 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));但它也抱怨焦炭*转换。在其他尝试中,我尝试编写函数将数组转换为向量,但是数组必须在其维度中进行数值定义,没有变量或留给用户以后定义的未指定的变量,例如: std::vector >array_convertto_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] != '\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<<" ";
}
}
}
}如果代码不够清晰,您必须取消向量部分的注释,并在我选定的三行中注释掉数组部分,以使其工作。
有一个部分,我在文件中加载了文字,所以我不得不取消对该部分的评论。如果它仍然没有编译,让我知道,我会重新发布一个有希望的工作版本的代码。谢谢你的帮助!
发布于 2013-08-16 03:34:56
你需要初始化你的字搜索向量,以便它是正确的大小。否则,您的程序将尝试使用未分配的内存,并且(通常)会得到段错误。
您可以在http://en.cppreference.com/w/cpp/container/vector/vector上使用第二个构造函数将向量初始化到一定的大小。
请记住,您也需要初始化“内部”向量。
对于特定的用例,正确的代码是:
std::vector<std::vector<char> > wordsearch(10,std::vector<char>(11));
请注意,您正在用长度为11的10个向量初始化字搜索向量。
https://stackoverflow.com/questions/18265303
复制相似问题