从我的向量打印的项目数与向量中假定的项目数不一致

时间:2014-06-19 21:08:35

标签: c++ vector iterator

我正在制作一个单词搜索游戏,而且我一直试图从已经被洗牌的预先存在的vector中获取一组随机数字。我可以这样做,但是当我遍历vector并打印字符串时,字符串的数量与numWords返回的值不一致,这可能会返回vector中的项目数。 / p>

我一直在看着它,但我无法弄清楚为什么它在大多数时候都不一致。此外,我试图在THE_SET中获得一组固定的8个单词,但不一致性促使我尝试随机数字,但它仍然不一致。如果我有办法让THE_SET始终有8个随机单词,我想知道。

**编辑:我已经解决了问题,谢谢。 [:我现在也明白了。我只是不知道我是否混淆了调试与否...因为我点击" Debug"检查错误并运行我的文件。呵呵。我还是一个初学者和练习者,所以我忘记了一些我读过的东西...如果我误解了任何人,那就很抱歉。

其他一切似乎都运转良好。

// word search.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <vector>
#include <cctype>
#include <iterator>

using namespace std;

void dispBoard(const vector<string>& wordSet);
void checkGuess(string entry, const vector<string>& wordSet);
int numWords(const vector<string>& wordSet);

string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int numLeft = 0;

int main()
{
    srand(static_cast<unsigned int>(time(0)));


    random_shuffle(alphabet.begin(), alphabet.end());

    vector<string> wordSets;
    wordSets.push_back("CHICKEN");
    wordSets.push_back("BEEF");
    wordSets.push_back("DISHONOR");
    wordSets.push_back("LAWNMOWER");
    wordSets.push_back("LEGEND");
    wordSets.push_back("PROGRAMMING");
    wordSets.push_back("DEVELOPER");
    wordSets.push_back("HOMEWORK");
    wordSets.push_back("TERRIBLE");
    wordSets.push_back("VACATION");
    wordSets.push_back("PYTHON");
    wordSets.push_back("RUBY");
    wordSets.push_back("POKEMON");
    wordSets.push_back("BORDERLANDS");
    wordSets.push_back("INFINITE");
    wordSets.push_back("SMASH");
    wordSets.push_back("BROTHERS");
    wordSets.push_back("SNAKES");
    wordSets.push_back("HAMSTER");
    wordSets.push_back("ELEPHANT");
    wordSets.push_back("BUFFALO");
    wordSets.push_back("PILLOW");
    wordSets.push_back("PASTA");
    wordSets.push_back("RAMEN");

    random_shuffle(wordSets.begin(), wordSets.end());
    vector<string> THE_SET(wordSets.begin(), wordSets.begin() + (rand() % 15 + 1)); //vector of current word set, formed from wordSets
    //I'd like this to be fixed at 8 words
    int MAX_WORDS = numWords(THE_SET);  //reinitialization to number of words in THE_SET--why is it inconsistent with printed words???
    numLeft = numWords(THE_SET);

    cout << "number of words left: " << numLeft << " and MAX WORDS: " << MAX_WORDS << "\n\n";   //checks num of words

    vector<string>::iterator iter;

    for(iter = THE_SET.begin(); iter != THE_SET.end(); iter++)      //prints words in THE_SET--but number of words counted by hand are usually 
    {                                                               //inconsistent
        cout << *iter << " ";
    }

    cout << "\n\n";
    string entry; 


    do {            //main loop of game
        dispBoard(THE_SET);
        cout << "You have " << numLeft << " words left to find. \n\n";
        cout << "(Enter 'EXIT' if you wish to quit the game.)\n";
        cout << "Enter your find in UPPERCASE: ";
        cin >> entry;
        cout << "\n\n";
        if(entry == "EXIT") {
            return 0;
        }
        else {
            checkGuess(entry, THE_SET);
        }

    } while(numLeft != 0);

    cout << "Congrats! You found all " << MAX_WORDS << " words!!\n\n";

    system("PAUSE");
}

void dispBoard(const vector<string>& wordSet) { //this displays a bunch of random letters around the words in THE_SET
    vector<string>::const_iterator iter;
    for(iter = wordSet.begin(); iter != wordSet.end(); iter++) 
    {
        cout << alphabet.substr(rand() % 8, (rand() % 32 + 1)) << *iter << alphabet.substr(rand() % 8, (rand() % 32 + 1)) << endl;
    }
}

void checkGuess(string entry, const vector<string>& wordSet) {
    if(entry == "EXIT")
    {
        cout << "Quitting game.\n\n";
        return;
    }
    else
    {
        vector<string>::const_iterator iter;
        for(iter = wordSet.begin(); iter != wordSet.end(); iter++) {
            if(entry == *iter)
            {
                cout << "That's right! " << entry << " is in the search!\n\n";
                numLeft--;
            }

        }
    }
}

int numWords(const vector<string>& wordSet)     //supposed to count number of strings in a given vector, but it's inconsistent. Why??
{
    unsigned int i = 0;
    vector<string>::const_iterator iter;
    for(iter = wordSet.begin(); iter != wordSet.end(); iter++)
    {
        i++;
    }
    return i;
}

2 个答案:

答案 0 :(得分:3)

执行这些行时:

vector<string> wordSets(24);
wordSets.push_back("CHICKEN");

wordSets项目24"CHICKEN"个空项目。我怀疑,你并不是那个意思。将第一行更改为:

vector<string> wordSets;

应该修复错误的项目数问题。

如果您希望通过调用push_back来减少分配内存的次数,可以使用:

vector<string> wordSets;
wordSets.reserve(24);

这将确保wordSets有足够的内存来容纳24个对象。

答案 1 :(得分:2)

详细说明 R Sahu 的回答:写作

vector<string> wordSets(24);

相当于

vector<string> wordSets;
wordSets.resize(24);

即。 wordSet 被初始化为具有24个元素的向量,您可以使用operator[]进行访问。通过push_back向向量添加元素会将向量的大小增加到25。

你可能想做的事情是为24个字符串保留足够的空间,以便向量不需要调整大小并为前24次添加重新分配内存。您可以按如下方式实现此目的:

vector<string> wordSets;
wordSets.reserve(24);

另请查看reserve()上的resize()cplusplus.com's reference of std::vector