xiekewang 2017-01-12 05:50 采纳率: 0%
浏览 1104

c++中this 为0x1c,自定义的类Vector<string>读取文件内容时报错

主程序如下,当从cin中读入任何数据都没有问题
int main()
{
StrVec sv{ "i","dont","understand" };
for (auto p = sv.begin(); p != sv.end(); p++)
cout << p << " ";
cout << endl;
cout << sv.size() << " " << sv.capacity() << endl;
string s;
while (getline(cin, s)) {
sv.push_back(s);
for (auto p = sv.begin(); p != sv.end(); p++)
cout << *p << " ";
cout << endl;
cout << sv.size() << " " << sv.capacity() << endl;
}
这是试运行效果。
图片说明
但是读取文件中的内容时就出问题:
图片说明
int main()
{
/*StrVec sv{ "i","dont","understand" };
for (auto p = sv.begin(); p != sv.end(); p++)
cout << *p << " ";
cout << endl;
cout << sv.size() << " " << sv.capacity() << endl;
string s;
while (getline(cin, s)) {
sv.push_back(s);
for (auto p = sv.begin(); p != sv.end(); p++)
cout << *p << " ";
cout << endl;
cout << sv.size() << " " << sv.capacity() << endl;
}
/
ifstream in("test.txt");
if (!in) {
cout << "无法打开输入文件" << endl;
EXIT_FAILURE;
}
runQueries(in);
system("pause");
return 0;
}

查看调用堆栈,感觉问题应该是在这里
TextQuery::TextQuery(ifstream &in):vs()
{
string line, word;
while (getline(in, line))
vs.push_back(line);****查看堆栈返回到这里***
for (size_t i = 0; i != vs.size(); i++) {
stringstream sm(*(vs.begin()+i));
while (sm >> word)
m[word].push_back(i + 1);//绑定每个单词和它出现的行号。
}
}
这个问题是C++Primer 5th里第十三章的13.42里的题。
(在你的TextQuery和QueryResult类中用StrvVec类代替vector)

经过反复的测验,我认为可以确定StrVec类是没有问题,但是不明白我定义的push_back读取cin时没有问题,读取文件时就出现问题。

  • 写回答

1条回答 默认 最新

  • devmiao 2017-01-13 16:32
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题