字符串流的输入问题

九个太阳2023 2008-02-27 03:32:15
该程序是对remainder.txt文件中的成对字符串进行操作,对于第一个字符串,要去掉两个字符串中相同的部分,并按字符顺序输出
remainder.txt文件内容为:
computer program
Write program
thatwill askthe
whole number

******************************
程序如下:
#include <iostream>
#include <algorithm>
#include <fstream>

using namespace std;

int main()
{
ifstream in("remainder.txt");

for (string s,t,u;in>>s>>t;u="")
{
sort(s.begin(),s.end());
sort(t.begin(),t.end());
set_difference(s.begin(),s.end(),t.begin(),t.end(),back_inserter(u));

cout<<u<<endl;
}

return 0;
}

编译的时候出现如下错误:
Deleting intermediate files and output files for project 'ch0515 - Win32 Debug'.
--------------------Configuration: ch0515 - Win32 Debug--------------------
Compiling...
ch0515.cpp
e:\my program\ch05\ch0515.cpp(11) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversi
on)
e:\my program\ch05\ch0515.cpp(17) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversi
on)
Error executing cl.exe.

ch0515.exe - 2 error(s), 0 warning(s)

请高手指点~~
...全文
176 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hastings 2008-02-28
  • 打赏
  • 举报
回复
注意:VC6的string类没有push_back()函数,所以
须自己创建一个.
hastings 2008-02-28
  • 打赏
  • 举报
回复
7楼代码在VC6上通过.
Great_qiang 2008-02-28
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ifstream in("remainder.txt");

for (string s,t,u;in >> t >>s;u="")
{
sort(s.begin(),s.end());
sort(t.begin(),t.end());
//set_difference(s.begin(),s.end(),t.begin(),t.end(),back_inserter(u)); 这句有了问题。

cout <<u <<endl;
}

return 0;
}
呵呵
九个太阳2023 2008-02-28
  • 打赏
  • 举报
回复
LS的代码还是没有解决问题,编译的时候还是出现了原来的错误
Deleting intermediate files and output files for project 'ch0515 - Win32 Debug'.
--------------------Configuration: ch0515 - Win32 Debug--------------------
Compiling...
ch0515.cpp
c:\program files\microsoft visual studio\vc98\include\iterator(82) : error C2039: 'push_back' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
c:\program files\microsoft visual studio\vc98\include\iterator(82) : while compiling class-template member function 'class std::back_insert_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &_
_thiscall std::back_insert_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::operator =(const char &)'
Error executing cl.exe.

ch0515.exe - 1 error(s), 0 warning(s)
hastings 2008-02-28
  • 打赏
  • 举报
回复
class String :public string
{
public:
void push_back(const char& c)
{
(*this).append(1,c);
}
};
int main(void)
{
ifstream in("remainder.txt");
for(String s,t,u;in>>s>>t;u.assign(""))
{
sort(s.begin(),s.end());
sort(t.begin(),t.end());
set_difference(s.begin(),s.end(),t.begin(),t.end(),std::back_inserter(u));
cout <<u <<endl;
}
return 0;
}
buzhihuigai 2008-02-27
  • 打赏
  • 举报
回复
“< <” 你的这个中间怎么会有空格?
九个太阳2023 2008-02-27
  • 打赏
  • 举报
回复
汗~~
跟编译器有关??
taodm 2008-02-27
  • 打赏
  • 举报
回复
你用的VC6啊?扔了吧。换VC2005express或者wxdevcpp
九个太阳2023 2008-02-27
  • 打赏
  • 举报
回复
好像还是不行啊~~
Deleting intermediate files and output files for project 'ch0515 - Win32 Debug'.
--------------------Configuration: ch0515 - Win32 Debug--------------------
Compiling...
ch0515.cpp
c:\program files\microsoft visual studio\vc98\include\iterator(82) : error C2039: 'push_back' : is not a member of 'basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
c:\program files\microsoft visual studio\vc98\include\iterator(82) : while compiling class-template member function 'class std::back_insert_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &_
_thiscall std::back_insert_iterator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::operator =(const char &)'
Error executing cl.exe.

ch0515.exe - 1 error(s), 0 warning(s)
taodm 2008-02-27
  • 打赏
  • 举报
回复
#include <string>

64,643

社区成员

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

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