在节点成员上使用向量函数

时间:2019-04-12 02:32:47

标签: c++ vector data-structures nodes

我正在用C ++编写程序,其中我从txt文件中提取一行并将其粘贴到结构内的vector<string> data中(我在制作每行的链接列表)。从技术上讲,我建立了一个循环,将要输入的句子中的单词分解开来,并将它们逐个推入向量中。

我遇到的问题是当我尝试通过以下方式获取矢量大小

int size;
size = current->data.size();

当前是一个节点。我同时收到隐式转换警告和“线程1:EXC_BAD_ACCESS(code = EXC_I386_GPFLT)”。

谁能解释我要去哪里错了?只是不可能编写类似的代码吗?我是否应该只创建一个计数器变量来跟踪在向量中放置了多少个单词?实现这一目标的最佳编程实践是什么?

这是我的main()文件


#include <iostream>
#include <fstream>
#include "SkipGram.hpp"

using namespace std;

int main() {
    string file;
    ifstream inFile;
    vector<string> sentence;
    string line;
    SkipGram control;
    int skip;
    int gram;

    cout << "Please enter file name:\n";
    cin >> file;
    inFile.open(file);

    while(!inFile.is_open()){   //makes sure we get a working file
        cout << "Error reading in file. Please try again.";
        cin >> file;
        inFile.open(file);
    }

    cout << "Please enter how many words you want skipped and the amount of grams:";
    cin >> skip >> gram;

    while(!inFile.eof()){
        getline(inFile, line);
        control.convert(line);
    }

    control.skipGramFunc(skip, gram);
    control.printSkipGram();

    return 0;



}

这是我的.hpp文件

#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class SkipGram{
public:
    SkipGram();
    void convert(string line);
    void skipGramFunc(int skip, int gram);
    void printSkipGram();

private:
    typedef struct sentence{
        vector<string> data;
        vector<string> result;
        int position;
        sentence* next;
    }* sentencePtr;

    sentencePtr first;
    sentencePtr current;
    int amount;
};

这是我的.cpp文件

#include "SkipGram.hpp"

using namespace std;

SkipGram::SkipGram(){
    first = NULL;
    current = NULL;
    amount = 0;
}

void SkipGram::convert(string line){
    // go word by word through sentence and create a vector out of it
    // add into the sentence list
    sentencePtr newSentence = new struct sentence;


    if(first == NULL){
        current = newSentence;
        first = newSentence;
        amount++;
        newSentence->position = amount;
    }else {
        current = newSentence;
        amount++;
        newSentence->position = amount;
    }

        string word;
        int length = line.length();
        int i = 0;
        int front = 0;
        int temp;

        while( i <= length){
            temp = line.find(" ");
            if( temp == -1){
                break; //catches when sentence is done with
            }
            word = line.substr(front, temp);
            newSentence->data.push_back(word);
            temp++;
            line = line.erase(front, temp);
        } //END OF WHILE

    }



void SkipGram::skipGramFunc(int skip, int gram){
    // goes through word vector and rearranges them
    if(gram == 1){
        cout << "Need more than one gram!" << endl;
        return;
    }

    if(skip == 0 || gram == 0){
        cout << "Input cannot be 0!";
    }

    int size;
    int temp;
    int tempAmount = amount;

    current = first;
    skip++;

    while( tempAmount != 0){ // while loop goes through all the sentences

        size = current->data.size();
        size = size - skip; // size here essentially becomes a marker to find out where to stop the loop

        for(int i = 0; i < size; i++){ // for loop goes through all the words

            for(int j = 0; j < gram; j++){ // this loop checks to see if we got the right number of grams
                if(j == 0){ // are we on the first gram
                    current->result.push_back(current->data.at(i));
                }else { // we want skipped gram
                    temp = i + skip;
                    current->result.push_back(current->data.at(temp));
                } // END OF IF

            }// END OF GRAM IF

            current->result.push_back(",");

        }//END OF WORD FOR

        current = current->next;
        tempAmount--;
    }//END OF WHILE

}// END OF FUNCTION

void SkipGram::printSkipGram(){


    int tempAmount = amount;

    current = first;

    while(tempAmount != 0){
        int size = current->data.size();

        for(int i = 0; i <= size; i++){
            cout << current->data.at(i);
        };

    }//END OF WHILE

}

1 个答案:

答案 0 :(得分:0)

您从未分配给visit_Array,但您确实已阅读。因此,您的程序通过访问未初始化对象的值而表现出未定义的行为。