集合未插入

Set is not inserting

本文关键字:插入 集合      更新时间:2023-10-16

我遇到了一个问题,我的代码在试图插入集合时似乎被卡住了。

using namespace std;
set<string> * lexset;

void build( const set<string>& word_list )
   set<string>::iterator it = word_list.begin();
   while( it != word_list.end()) 
   {
      cout << *it << "before"<< endl;
      lexset->insert(*it);
      cout << "after" <<endl;
      ++it;
      cout << "end of while" <<endl;
   }
}

它似乎卡在了"之后"打印出来的行之前。

插入之前的行打印出一个字符串,我确信单词列表中包含了很多单词。这是编译的,不会循环。如果我删除插入,它会很好地遍历。为什么这个插件会引起这么多麻烦?

首先为lexset:分配内存

lexset = new set<string>;

并修复您的word_list打字错误