2009-04-27 37 views
2

我正在写一个hang子手游戏。我的逻辑失败,无论是我自己还是我的游戏逻辑。字符猜测(人们猜测的字母)没有被添加到矢量guessArray的正确内存插槽中。假设该词是用户输入的词。std :: vector与C++中的原始数组有多类似?

我猜这会工作,如果guessArray是一个原始数组。有没有一些原因,这不是与矢量工作?

//assume that attempts is num of attempts left 
void coutWord(int attempts, std::string word, char guess) 
{ 
std::vector<char> guessArray(word.length()); 


//this is supposed to add the guess to the correct area of guessArray; 
//It's not. 

for (int i = 0; i < word.length(); i++) { 
    if (guess == word[i]) { 
     guessArray[i] = guess; 
     std::cout << " " << guessArray[i] << " "; 
     continue; 
    } 

    std::cout << " _ "; 
} 

std::cout << std::endl << std::endl; 
} 

编辑:我使用此代码的目的是来清点所有unguessed空间,在同一个for循环的猜测空间。我只需要“记住”以前的猜测,以便我得到正确的输出。定的字=“苹果酱”:

Input: a 
a _ _ _ _ _ a _ _ _ 
Input: p 
a p p _ _ _ a _ _ _ 

+2

这似乎是相当bug的代码,不管是否guessArray是一个载体或原始阵列。 为了能够回答你的问题,你需要说出你在这里尝试做什么。 EG,如果单词是“Triangulate”,并且猜测是“g”,那么输出应该是什么? – 2009-04-27 03:32:07

+0

那么我应该通过矢量作为另一个参数? – jkeys 2009-04-27 03:50:44

回答

3

载体可以用下标符号[]进行索引,并且它被存储在连续的存储器。它是一个STL容器,因此,就像数组一样,您可以拥有任何类型的容器。

矢量自动调整大小。一个数组是'静态'大小的,并且不能轻易调整大小(除了调用realloc的手动函数外)。您可以使用push_back函数来处理这个问题,并且还可以提前保留()内存以保存重新分配。

数组不会跟踪它自己的大小,而矢量具有可以检查它的函数。

如果您不确定矢量的大小,只需继续并使用.push_back()添加项目以处理自动调整大小的问题。如果通过resize()保留了一块内存然后索引到内存中,那么使用它作为一个数组更容易,但是您将失去将它用作动态大小对象的一些合成优势。

-1

你应该使用push_back()函数。详细信息请参见cpprefererence.com

而且,你不能简单地与C数组替换guessArray,因为你需要在编译时已知常数excplicitly指定其大小,像

int guessArray[25]; 
0

应答传送至编辑:

guessArray向量是在函数中本地创建的,因此,先前插入到向量中将不可用于下一次尝试。您需要通过引用传递向量,并为每次尝试更新向量。

void coutWord(std::vector<char>& guessArray, int attempts, std::string word, char guess) 

伪代码:

void coutWord(std::vector<char>& guessArray, int attempts, std::string word, char guess) 
{ 

    for (int i = 0; i < word.length(); i++) 
    { 
     if (guess == word[i]) 
     { 
      //its current guess 
      //insert and display as well 
      guessArray[i] = guess; 
      std::cout << " " << guessArray[i] << " "; 

     } 
     //Check if the previous attempt has already inserted the char (previous guess) 
     else if(guessArray[i] != 0) 
     { 
      //display previous guess too 
      std::cout << " " << guessArray[i] << " "; 

     } 
     else 
     { //not yet guessed 
      std::cout << " _ "; 
     } 
    } 

    std::cout << std::endl << std::endl; 
} 

外面定义矢量:

std::vector<char> guessArray(word.length()); 

    coutWord(guessArray, 1, word, 'a'); 
2

有在你的代码的基本逻辑上的缺陷,无法使用向量或阵列。

有你想在这里做两项工作:

  • 更新猜测的数组
  • 输出猜测的阵列

人们很容易搞混,而你试图在一个函数中完成这两项任务。我对你的建议是把它们分成不同的功能。

这是一个基本的代码结构(使用功能,你可以实现):

int attempts = 0; 
std::vector<char> guessArray(word.length()); 
while((attempts > maxAttemps) && (!HasFoundWord(guessArray)) 
{ 
    char guess = InputGuess(); 
    UpdateResults(guessArray, guess, word); 
    OutputGuess(guessArray); 
    ++attempts; 
} 

的UpdateResults将有一个像函数签名:

void UpdateResults(std::vector<char>& guessArray, char guess, const std::string& word) 

一旦分离出来的功能件,你会发现这个问题要更直接地解决。

0

我很惊讶还没有人提到这个问题,但性病::向量不是一个数组。这是一个调整大小的连续的元素容器,可用于与数组很相同的目的。如果你想有一个包裹阵列(也有任意数量的原因),你应该看看boost::array

相关问题