C++ 如何将对象添加到堆栈上的向量数组?
我目前正在创建一个帮助我学习新词汇的程序。该程序的工作流程将由VocabularyTester类控制,它包含存储在向量数组中的WordPair类对象列表C++ 如何将对象添加到堆栈上的向量数组?,c++,arrays,oop,C++,Arrays,Oop,我目前正在创建一个帮助我学习新词汇的程序。该程序的工作流程将由VocabularyTester类控制,它包含存储在向量数组中的WordPair类对象列表 class VocabularyTester { private: std::vector<WordPair> wordList; std::vector<WordPair> failedPairs; std::vector<WordPair> passedPairs; Wor
class VocabularyTester
{
private:
std::vector<WordPair> wordList;
std::vector<WordPair> failedPairs;
std::vector<WordPair> passedPairs;
WordPair recentPair;
public:
VocabularyTester();
void run();
void loadWordList(std::wstring& vocabularyRaw);
/*
other parts of the code
*/
};
/* the function i'm having a problem with */
void VocabularyTester::loadWordList(std::wstring& vocabularyRaw)
{
std::wstring rawPair;
unsigned int iterator = 0, prevIterator = 0;
for (;;)
{
iterator = vocabularyRaw.find(L"\r\n", iterator);
if (iterator == std::string::npos)
{
break;
}
rawPair = vocabularyRaw.substr(prevIterator, iterator);
wordList.emplace_back(rawPair); /*there occurs the read access violation exception*/
prevIterator = iterator;
}
}
WordPair是一对两个单词,它有两个字符串成员:leftWord代表母语中的一个单词,rightWord代表有人想学习的语言中的一个单词。下面是我用来将其添加到向量数组的对象的构造函数:
WordPair::WordPair(std::wstring& rawPair)
{
std::wstring separator = L" - ";
size_t separatorPosition = rawPair.find(separator);
std::wstring leftWord = rawPair.substr(0, separatorPosition);
std::wstring rightWord = rawPair.substr(separatorPosition + separator.length());
this->leftWord = leftWord;
this->rightWord = rightWord;
}
谢谢你的帮助。什么是
字对
?你能试着创建一个并向我们展示吗?\u MyEnd()
和\u MyLast()
的代码在哪里?@Bigiansen该代码可能来自标准库实现内部,实际上与此无关。问题将出现在OP编写的代码中,但尚未向我们展示。我已经在问题中添加了对单词对的解释。我认为是时候进行分析了
WordPair::WordPair(std::wstring& rawPair)
{
std::wstring separator = L" - ";
size_t separatorPosition = rawPair.find(separator);
std::wstring leftWord = rawPair.substr(0, separatorPosition);
std::wstring rightWord = rawPair.substr(separatorPosition + separator.length());
this->leftWord = leftWord;
this->rightWord = rightWord;
}