小编011*_*813的帖子

函数调用后字符串更改

我可能犯了一个非常明显的错误,但我看不到它,我希望有人可以帮助我.

我想在我的C++程序中将2个字符串添加到对向量中.容器看起来像vector< pair < word , type > > vec.这个词可以是任何东西,类型是名词,形容词,动词等.例如< Hogwarts , noun>,< Hungry, adjective>等等.

这是我在单词bank中添加单词的地方.FileStrings只是从File I/O创建的另一个容器.这里一切都很好.

for(unsigned int i = 0; i < fileStrings.size(); ++i)
{
    cout << "Adding " << fileStrings[i] << ", " << fileStrings[i + 1] << endl;
    theWordBank.addWord(fileStrings[i], fileStrings[++i]);
}
Run Code Online (Sandbox Code Playgroud)

这是我迷路的地方.打印出newWord时,它与newType完全相同.我无法理解它,我不明白newWord如何从上面的代码片段中失去它的价值.

void addWord(const string& newWord, const string& newType)
{
    mWords.push_back(make_pair(newWord, newType));
    std::cout << "newWord " << newWord << ", " << newType << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

再一次,结果应该是类似的< Hogwarts, noun> …

c++

1
推荐指数
1
解决办法
57
查看次数

标签 统计

c++ ×1