List用来存储string时遇到的问题
//输入10个单词作为字典,用list有序存放,并输出最终结果
#include <iostream>
#include <list>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
template <class T>
class OrderList
{
public:void InsertOrder (T newword);
void Showdic ( void );
private:list<T> dicList;
};
template <class T>
void OrderList<T>::InsertOrder (T newword)
{
list<T>::iterator currPtr = dicList.begin();
list<T>::iterator nextPtr = dicList.begin();
while (nextPtr != dicList.end())
{
currPtr = nextPtr;
nextPtr++;
}
if (newword > *currPtr)
{
dicList.push_back(newword);
return;
}
for ( currPtr = dicList.begin(); currPtr != dicList.end(); currPtr++)
{
if (newword < *currPtr)
{
dicList.insert(currPtr,newword);
break;
}
}
}
template <class T>
void OrderList<T>::Showdic (void)
{
cout << endl << "Now showing dictionary:" << endl;
for (list<T>::iterator ps = dicList.begin(); ps != dicList.end(); ps++)
{
cout << *ps << " ";
}
}
int main ( )
{
OrderList<string> strlist;
for (int i = 0; i < 10; i++)
{
string str;
cout << endl << "Enter a word: " << flush;
cin >> str;
strlist.InsertOrder(str);
}
strlist.Showdic();
system("pause");
return 0;
}
1.在VC++6.0上:
总是给出警告.运行时输入第一个单词后就出现非法操作提示.
如果用int代替string测试则不警告.并且运行正常
2.在DEV-CPP4.9.8.0下某一台机上曾有一次编译完全正确.并且运行正常
请指出出错原因并给出修改方案.