在节点成员上使用向量函数 我在C++中编写一个程序,在这里我从一个TXT文件中输入一行,并将其插入到一个结构内的矢量数据< /代码>(我正在制作每个单独的行的链表)。从技术上讲,我已经建立了一个循环,它将把我正在接受的句子中的单词分解,并将它们一个一个地推到向量中
我遇到的问题是当我尝试通过在节点成员上使用向量函数 我在C++中编写一个程序,在这里我从一个TXT文件中输入一行,并将其插入到一个结构内的矢量数据< /代码>(我正在制作每个单独的行的链表)。从技术上讲,我已经建立了一个循环,它将把我正在接受的句子中的单词分解,并将它们一个一个地推到向量中,c++,vector,data-structures,nodes,C++,Vector,Data Structures,Nodes,我遇到的问题是当我尝试通过 int size; size = current->data.size(); 当前是一个节点。我得到一个隐式转换警告和一个“线程1:EXC_BAD_访问(code=EXC_I386_GPFLT)” 谁能解释一下我哪里出了问题?难道不可能编写这样的代码吗?我是否应该创建一个计数器变量来跟踪向量中放置了多少单词?实现这一目标的最佳编程实践是什么 这是我的主()文件 #包括 #包括 #包括“SkipGram.hpp” 使用名称空间std; int main(){
int size;
size = current->data.size();
当前是一个节点。我得到一个隐式转换警告和一个“线程1:EXC_BAD_访问(code=EXC_I386_GPFLT)”
谁能解释一下我哪里出了问题?难道不可能编写这样的代码吗?我是否应该创建一个计数器变量来跟踪向量中放置了多少单词?实现这一目标的最佳编程实践是什么
这是我的主()文件
#包括
#包括
#包括“SkipGram.hpp”
使用名称空间std;
int main(){
字符串文件;
河流充填;
向量句;
弦线;
SkipGram控制;
整数跳跃;
整数克;
cout>文件;
inFile.open(文件);
虽然(!infle.is_open()){//确保我们得到一个工作文件
cout>文件;
inFile.open(文件);
}
不能>跳过>>克;
而(!infle.eof()){
getline(填充,行);
控制。转换(行);
}
对照.skipGramFunc(skip,gram);
控件printSkipGram();
返回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;
};
#包括
#包括
#包括
#包括
使用名称空间std;
课堂技巧{
公众:
SkipGram();
无效转换(字符串行);
void skipGramFunc(int skip,int gram);
void printSkipGram();
私人:
typedef结构句{
矢量数据;
矢量结果;
内部位置;
下一句*;
}*句子ptr;
句子优先;
句子电流;
整数金额;
};
这是我的.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
}
#包括“SkipGram.hpp”
使用名称空间std;
SkipGram::SkipGram(){
第一个=空;
电流=零;
金额=0;
}
void SkipGram::convert(字符串行){
//逐字逐句地浏览句子,并从中创建一个向量
//添加到句子列表中
sentencePtr newSentence=新结构句子;
if(first==NULL){
当前=新闻事件;
第一个=新闻事件;
金额++;
新闻事件->职位=金额;
}否则{
当前=新闻事件;
金额++;
新闻事件->职位=金额;
}
字符串字;
int length=line.length();
int i=0;
int-front=0;
内部温度;
while(i data.push_back(word));
temp++;
行=行擦除(前,温度);
}//时间结束
}
void SkipGram::skipGramFunc(int skip,int gram){
//遍历单词向量并重新排列它们
如果(克=1){
cout data.at(i));
}否则{//我们想要一个
温度=i+跳过;
当前->结果。向后推(当前->数据。温度);
}//如果结束
}//克末IF
当前->结果。推回(“,”;
}//词尾
当前=当前->下一步;
数量--;
}//结束
}//功能结束
void SkipGram::printSkipGram(){
int tempAmount=金额;
电流=第一;
while(tempAmount!=0){
int size=current->data.size();
对于(int i=0;i您从未分配给next
,但您确实读取了它。因此,您的程序通过访问未初始化对象的值来显示未定义的行为。当前和数据的定义是什么?我假设数据
是std::vector
。请提供一个,其中包括main()和all include,这样成员就可以复制并粘贴到IDE中。是的,数据是一个向量,current是一个结构。我也在代码中添加了。噢!很好!非常感谢!
#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
}