繁体   English   中英

访问指针向量

[英]Accessing a vector of pointers

这是用于CS分配的。 我看过类似的问题/问题,但似乎找不到答案。

一些背景信息:我们需要对列表中的c字符串进行排序,并计算每个单词出现的次数。 解决问题的必要条件:编写一个程序Problem2_A.cpp,该程序从键盘以C字符串形式输入多行文本,并按字母顺序列出该单词的每个单词及其出现的次数。 为此,请将指针指向数组中每个单词。 使用qsort()函数对指针数组进行排序,然后计算每个单词的出现次数。 通过发信号通知键盘结束文件来终止输入。 忽略大小写差异(例如,“ Cat”和“ cat”是同一个词)。 最低要求是,您可以假定将没有标点符号,并且仅一个空格将输入行中的单词分隔开。 您可以删除此限制作为扩展。

编辑-我知道我需要用户输入一行单词。 现在,我将只手动输入每个单词,因为我更关心访问向量和检索要检索的内容。

这是我到目前为止的

Word.H

#pragma once
#ifndef __WORD_H__
#define __WORD_H__
#define _CRT_SECURE_NO_WARNINGS

class Word {
public:
    Word(char * word);

    ~Word();

    /* Returns the inputted word */
    const char* getWord();

private:
    char word[51];
};

#endif

Word.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "Word.h"

Word::Word(char * word) {
    strcpy(this->word, word);
}

Word::~Word() {

}

const char* Word::getWord() {
    char* clone = new char[50];
    strcpy(clone, word);
    return clone;
}

Problem2.cpp

#include <iostream>
#include <vector>
#include "Word.h"

using namespace std;

const int MAX_WORDS = 2;
const int MAX_WORD_LENGTH = 15;

char* promptWord();


int main() {
    char inputWord[MAX_WORD_LENGTH];
    vector<Word*> pWordList;
    pWordList.reserve(MAX_WORDS);

    cout << "Please type each word and press enter: ";

    for (int i = 0; i < MAX_WORDS; i++) {
        Word* newWord = new Word(promptWord());
        pWordList.push_back(newWord);
    }

    cout << "Printing &pWordList[i]" << endl;
    for (size_t i = 0; i < pWordList.size(); i++) {
        cout << &pWordList[i] << endl;
    }

    cout << "Printing (void*)pWordList[i]->getWord()" << endl;
    for (size_t i = 0; i < pWordList.size(); i++) {
        cout << (void*)pWordList[i]->getWord() << endl;
    }

    cout << "Printing pWordList[i]->getWord()" << endl;
    for (size_t i = 0; i < pWordList.size(); i++) {
        cout << pWordList[i]->getWord() << endl;
    }

    cout << "Printing (*pWordList[i]).getWord()" << endl;
    for (size_t i = 0; i < pWordList.size(); i++) {
        cout << (*pWordList[i]).getWord() << endl;
    }

    /* Delete our dynamic objects */
    while (!pWordList.empty()) {
        delete pWordList.back();
        pWordList.pop_back();
    }




    system("pause");
    return 0;
}

/*
Ask the user to enter their desired word
@params inputWord[MAX_WORD_LENGTH] -> Desired word to be inputted
@return Returns the user's inputted word
*/
char* promptWord() {
    char inputWord[MAX_WORD_LENGTH];
    cin.getline(inputWord, MAX_WORD_LENGTH);
    return inputWord;
}

我对指针从来都不是太擅长,但是如果我理解正确的话,我目前拥有的是指向我动态分配的Word对象位置的指针向量。

我的部分困惑可能来自这个问题-我是否需要将对象放入指针向量中? 还是我将指针指向对象并将这些指针放在指针向量中?

无论如何,如您所见,我正在尝试找出输出单词的正确方法。 当我称它为

cout << &pWordList[i] << endl;

这将为每个单词(单词的指针?)输出一个位置地址。 运用

cout << (void*)pWordList[i]->getWord() << endl;

将输出一个不同的位置(指向向量索引的指针?)

和使用

cout << pWordList[i]->getWord() << endl;

cout << (*pWordList[i]).getWord() << endl;

将打印出相同的垃圾。

我很可能做错了,因为

pWordList[i]->getWord()

应该打印出有问题的c字符串,但是我认为它在传输中丢失了,并且指针位置变为无效。 确实会提供任何建议或意见。

Edit2--如Colin Basnett和T33C所述,我已将功能更改为

/*
Ask the user to enter their desired word
@return Returns new dynamic char containing inputted word
*/
char* promptWord() {
    char* inputWord = new char[MAX_WORD_LENGTH];
    cin.getline(inputWord, MAX_WORD_LENGTH);
    return inputWord;
}

现在我可以输出正确的内容。 仍然想知道来自&pWordList [i]和(void *)pWordList [i]-> getWord()的两个不同位置

Edit3--这是我的方法,其中包含无效的地图。 它应该对向量进行排序,并使用单词(实际上是指向单词的指针)及其出现的次数来创建映射。 但是,它只是对其进行排序。 即使多次输入相同的单词,也会打印出出现的每个单词1。 这是因为它实际上是将指向单词的指针而不是单词本身插入键部分。

void measureVector(vector<Word*> ourVector, int numOfElements) {
    int count = 0;
    bool isDone = false;
    qsort(&ourVector[0], ourVector.size(), sizeof(Word*), wordCompare);

    map<Word*, int> wordCount;
    for (int i = 0; i < ourVector.size(); i++) {
        wordCount[ourVector[i]]++;
    }

    for (auto const& wc : wordCount) {
        cout << wc.first->getWord() << " appears " << wc.second << " times." << endl;
    }

}

我尝试改变

for (int i = 0; i < ourVector.size(); i++) {
    wordCount[ourVector[i]]++;
}

for (int i = 0; i < ourVector.size(); i++) {
    wordCount[ourVector[i]->getWord()]++;
}

但是,这说明了错误

'[': no operator found which takes a right-hand operand of type 'const char *' (or there is no acceptable conversion)

Edit4--我再次给我的教授发电子邮件,看看是否创建字符串映射并将char *转换为字符串,他说我可以,因此这是更新的measureVector方法,以防其他人遵循此方法。

/*
    Sorts our vector of pointers and puts all words into a map and
    keeps track of each word's occurrence.
    @params inputVector -> The vector of pointers to utilize.

*/
void measureVector(vector<Word*> inputVector) {
    /* Sort our vector of pointers */
    qsort(&inputVector[0], inputVector.size(), sizeof(Word*), wordCompare);
    map<string, int> wordCount;
    string ourString;

    /* For each word in our vector, insert it into our map. */
    for (auto word : inputVector) {
        ourString = word->getWord();
        wordCount[ourString]++;
    }

    /* For each unique word in our map, print out the word and how many times it occurred.*/
    for (const auto &p : wordCount) {
        cout << p.first << " appears " << p.second << " time" << (p.second > 1 ? "s." : ".") << endl;
    }
}

谢谢大家的指导!

这段代码中有很多问题,但问题的关键在于,您正在返回一个指向堆栈缓冲区的指针,该缓冲区在函数返回后不再存在。 函数返回展开其堆栈帧。

char* promptWord() {
    char inputWord[MAX_WORD_LENGTH];
    cin.getline(inputWord, MAX_WORD_LENGTH);
    return inputWord;
}

我会犹豫地说,在堆上声明缓冲区并返回指向该缓冲区的指针,因为整个代码与现代C ++的位置相去甚远。

我建议使用:

  1. std :: string并返回它而不是char *
  2. 切勿在智能指针(或保证调用delete的东西)之外使用new,请参阅std :: unique_ptr,但在您的情况下,std :: string是更好的选择。

一切顺利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM