std::vector 与 C++ 中的原始数组有多相似?
我正在写一个刽子手游戏。 我的逻辑失败了,无论是我自己还是我的游戏逻辑。 字符猜测(人猜测的字母)没有被添加到向量guessArray的正确内存槽中。 假设该单词是用户输入的单词。
我认为如果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;
}
编辑:我使用此代码的目标是在同一 for 循环中计算所有未猜测的空间和猜测的空间。 我只需要“记住”之前的猜测,以便获得正确的输出。 给定单词 =“Applesauce”:
Input: a
a _ _ _ _ _ a _ _ _
Input: p
a p p _ _ _ a _ _ _
等等。
I'm writing a hangman game. I'm having a logic fail, both with myself and my game logic. Char guess (the letter the person guessed) isn't being added into the correct memory slot of the vector guessArray. Assume that word is an inputted word by the user.
I assume this would work if guessArray were a raw array. Is there some reason this isn't working with a vector?
//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;
}
EDIT: My objective with this code is to cout all the unguessed spaces AND the guessed spaces in the same for loop. I just need to "remember" previous guesses so that I get correct output. Given word = "Applesauce":
Input: a
a _ _ _ _ _ a _ _ _
Input: p
a p p _ _ _ a _ _ _
etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
向量可以用下标符号
[]
来索引,并且它存储在连续的内存中。 它是一个 STL 容器,因此,就像数组一样,您可以拥有任何类型之一。矢量会自动调整大小。 数组是“静态”大小的,并且不能轻易调整大小(调用 realloc 的手动函数除外)。您可以使用 Push_back 函数来处理此问题,也可以提前 .reserve() 内存来保存关于重新分配。
数组不跟踪它自己的大小,而向量具有可以检查它的函数。
如果您不确定向量的大小,请继续使用 .push_back() 添加项目来处理自动调整大小的问题。 如果您通过 resize() 保留一块内存,然后对其进行索引,则更容易将其用作数组,但您会失去将其用作动态大小对象的一些语法优势。
A vector can be indexed with subscript notation
[]
, and it is stored in contiguous memory. It is an STL container so, like an array, you can have one of any type.A vector is automatically resized. An array is 'statically' sized, and cannot be easily resized (with the exception of a manual function called to realloc.) You can use a push_back function to handle this, and you can also .reserve() memory ahead of time to save on reallocation.
An array does not track it's own size, whereas a vector has functions that can check this.
If you're unsure of the size of a vector, just go ahead and use .push_back() to add items to handle the matter of automatically sizing. If you reserve a chunk of memory through resize() and then index into it, it's easier to use as an array, but you lose some of the syntatic benefit of using it as a dynamically-sized object.
除了使用向量或数组之外,您的代码中还存在基本的逻辑缺陷。
您在这里尝试执行两项任务:
当您尝试在一个函数中执行这两项任务时,很容易混淆。 我给你的建议是将它们放入单独的函数中。
下面是一个基本的代码结构(使用您可以实现的函数):
UpdateResults 将具有如下函数签名:
一旦您分离出功能块,您就会发现问题更容易解决。
There are fundamental logical flaws in your code, beyond the use of vector or arrays.
There are two tasks you are trying to do here:
It's easy to get mixed up while you're attempting to do both tasks in the one function. My suggestion to you is to put these into separate functions.
Here's a basic code structure (using functions that you can implement):
The UpdateResults would have a function signature like:
Once you have separated out the pieces of functionality, you'll find the problem a lot more straightforward to solve.
对编辑的回答:
guessArray 向量是在函数中本地创建的,因此,先前插入到向量中的操作将无法用于下一次尝试。 您需要通过引用传递向量并为每次尝试更新向量。
伪代码:
在外部定义向量:
Answer to the EDIT:
guessArray vector is created locally in the function and hence, previous insertion into the vector will not be available for next attempt. You need to pass the vector by reference and update the vector for every attempt.
Pseudo code:
define vector outside:
我很惊讶还没有人提到它,但 std::vector 不是数组。 它是一个可调整大小的连续元素容器,可用于许多与数组相同的用途。 如果您想要一个包装数组(并且有很多原因),您应该查看 boost::array
I'm surprised nobody has mentioned it yet, but std::vector isn't an array. It's a resizing contiguous container of elements that can be used for a lot of the same purposes as an array. If you want a wrapped array (and there are any number of reasons to), you should look at boost::array
您应该使用push_back()函数。 有关详细信息,请参阅 cpprefererence.com。
另外,你不能简单地用 C 风格的数组替换guessArray,因为你需要使用编译时已知的常量显式指定其大小,例如
You should use push_back() function. See cpprefererence.com for details.
Also, you cannot simply replace guessArray with C-style array, because you need to excplicitly specify its size with constant known at compile-time, like