关于一道文本语句输出的问题

都说没想好 2014-05-10 02:34:32


显示R6010 -abort() has been called
我只是在源代码的基础上把vector改成了List
要访问其中元素的时候就用了迭代器,分别修改了2出(下面有用中文标明)
测试数据是:
<none> cat
<none> dog
<none> table
<nonephrase> <none>
<nonephrase> <adj> <nonephrase>
<adj> large
<adj> brown
<adj> absurd
<verb> jumps
<verb> sits
<position> on the stairs
<position> inder the sky
<position> wherever it wants
<sentence> the <nonephrase> <verb> <position>

求修改让他运行成功。。。
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include <list>

#include "split.h"
#include <time.h>

using namespace std;


typedef list <string> Rule;
typedef list <Rule> Rule_collection;
typedef map<string, Rule_collection> Grammar;

// read a grammar from a given input stream
Grammar read_grammar(istream& in)
{
Grammar ret;
string line;

// read the input
while (getline(in, line)) {

// `split' the input into words
list <string> entry = split(line);

//修改了这里1
if (!entry.empty())
{
list<string>::iterator iter=entry.begin();
// use the category to store the associated rule
ret[*iter].push_back(
Rule(++iter, entry.end()));
}
}
return ret;
}

void gen_aux(const Grammar&, const string&, list<string>&);

int nrand(int);

list<string> gen_sentence(const Grammar& g)
{
list<string> ret;
gen_aux(g, "<sentence>", ret);
return ret;
}

bool bracketed(const string& s)
{
return s.size() > 1 && s[0] == '<' && s[s.size() - 1] == '>';
}

void
gen_aux(const Grammar& g, const string& word, list<string>& ret)
{

if (!bracketed(word)) {
ret.push_back(word);
} else {
// locate the rule that corresponds to `word'
Grammar::const_iterator it = g.find(word);
if (it == g.end())
throw logic_error("empty rule");

// fetch the set of possible rules
const Rule_collection& c = it->second;
// from which we select one at random
//修改了这里2
Rule_collection::const_iterator ito=c.begin();
int num=nrand(c.size());
while(num--)
ito++;
const Rule& r = *ito;
// recursively expand the selected rule
for (Rule::const_iterator i = r.begin(); i != r.end(); ++i)
gen_aux(g, *i, ret);
}
}

int main()
{
// generate the sentence
list<string> sentence = gen_sentence(read_grammar(cin));

// write the first word, if any
list<string>::const_iterator it = sentence.begin();
if (!sentence.empty()) {
cout << *it;
++it;
}

// write the rest of the words, each preceded by a space
while (it != sentence.end()) {
cout << " " << *it;
++it;
}

cout << endl;
return 0;
}

// return a random integer in the range `[0,' `n)'
int nrand(int n)
{
if (n <= 0 || n > RAND_MAX)
throw domain_error("Argument to nrand is out of range");

const int bucket_size = RAND_MAX / n;
int r;

do r = rand() / bucket_size;
while (r >= n);

return r;
}
...全文
116 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
都说没想好 2014-05-11
  • 打赏
  • 举报
回复
没人回答吗。。。求答啊。。。

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧