将代码从数组移植到向量
Porting code from array to vector
我开发了一个简单的填字游戏生成器,其中重要的一点是我使用了一个数组作为网格。我现在正在做的是设置它,这样用户就可以选择他们想要使用的网格大小。对于数组,我无法做到这一点,所以我想更改代码以使用向量。我对此有困难,尝试了一些不同的方法,但无法使其发挥作用:
// 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矢量。
- 如何在向量数组中插入元素?
- 不同大小的向量数组的大小 cpp
- 很好的语法来获取对向量/数组数据的大小引用?
- 指向 c++ 中的向量数组并将其发送到函数的指针
- 初始化向量数组,其中每个向量的大小为 0
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 如何使用结构体的向量数组?
- C++:向量数组
- 在C++中通过引用传递向量数组
- 向量数组"Cannot access memory at address"
- 构造不同类型的C++向量数组
- 向量数组的均值和众数 - 如何对函数进行较小的改进
- C++:初始化(新)一个不同初始大小的向量数组
- 五个中三个最大参数的平均值,不使用排序算法或向量/数组
- 销毁C++中的向量数组
- 继承向量/数组/列表并更改其大小
- 如何输出属于向量数组的结构字符串
- 如何通过引用传递向量数组
- 在向量数组中获得前五个最大的结果
- 为什么"new vector<int>[5]"不创建向量数组?