关于 set::insert 的一个问题,在 VC++ 2003 下编译通过,在 Dev-C++ 中编译报错
这是一本书中的标准答案,而且在 VC++ 2003 下编译通过,但是在 Dev-C++ 中却编译报错,麻烦大家帮忙看看。
源程序如下:
#include <iostream>
#include <set>
#include <list>
#include <string>
#include <algorithm>
#include <iterator>
int main()
{
using std::string;
using std::cin;
using std::cout;
using std::ostream_iterator;
using std::copy;
using std::pair;
typedef std::set<string> WordSet;
typedef WordSet::iterator WordIter;
typedef std::list<string> Index;
WordSet words;
Index input_order;
for ( string new_word; cin >> new_word, new_word != "Quit"; )
{
pair<WordIter, bool> &trace = words.insert ( new_word );
if ( trace.second )
input_order.push_back ( *trace.first );
}
copy ( input_order.begin(), input_order.end(),
ostream_iterator<string> ( cout, "\n" ) );
copy ( words.begin(), words.end(), ostream_iterator<string> ( cout, "\n" ) );
return 0;
}
错误在这一行:
pair<WordIter, bool> &trace = words.insert ( new_word );
错误信息如下:
Ex05_11.cpp: In function `int main()':
Ex05_11.cpp:26: error: could not convert `std::set<_Key, _Compare,
_Alloc>::insert(const _Key&) [with _Key = std::string, _Compare =
std::less<std::string>, _Alloc = std::allocator<std::string>]((&new_word))'
to `std::pair<std::_Rb_tree_iterator<std::string, const std::string&, const
std::string*>, bool>&'