提问:使用find在一个int型的list中寻找值为0的最后一个元素

ysysbaobei 2008-11-20 11:37:04
题目要求:使用find在一个int型的list中寻找值为0的最后一个元素

请大家帮忙写出程序(p355/p369, page369_1.cpp)
...全文
166 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
就呆在云上 2008-11-20
  • 打赏
  • 举报
回复
给一个程序哈哈:
#include <list>
#include <algorithm>
#include <iostream>

int main() {
using namespace std;

list <int> L;
list <int>::iterator Iter;


L.push_back( 40 );
L.push_back( 20 );
L.push_back( 10 );
L.push_back( 30 );
L.push_back( 0 );

cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
cout << *Iter << " ";
cout << ")" << endl;

list <int>::reverse_iterator result;
result = find( L.rbegin( ), L.rend( ), 0 );
if ( result == L.rend( ) )
cout << "There is no 0 in list L.";
else {
cout << "There is a 0 in list L";
if ( ++result != L.rend() )
cout << " and it is followed by a " << *result << ".";
}
cout << endl;
}
qq7400264 2008-11-20
  • 打赏
  • 举报
回复
不错不错
ysysbaobei 2008-11-20
  • 打赏
  • 举报
回复
翻看了一下书本前面讲的 list容器里,find()的用法,写了一个程序

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
int x;
list<int> ilist;
while (cin >> x)
ilist.push_back(x);
list<int>::reverse_iterator rlter;
int search(0);
rlter = find(ilist.rbegin(), ilist.rend(), search); // p281/295
cout << *rlter << endl;
cout << *--rlter << endl; // 测试是否是最后一个0而用的;-- 指向 顺序方向的下一个元素

return 0;
}
taodm 2008-11-20
  • 打赏
  • 举报
回复
rbegin(), rend()
太乙 2008-11-20
  • 打赏
  • 举报
回复
一个while循环?为啥不用find_end呢?
ysysbaobei 2008-11-20
  • 打赏
  • 举报
回复
问题解决,谢谢大家
感觉是看到书本后面,忘了前面
结贴了
ysysbaobei 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ztz0223 的回复:]
给一个程序哈哈:

C/C++ code#include <list>
#include <algorithm>
#include <iostream>

int main() {
using namespace std;

list <int> L;
list <int>::iterator Iter;


L.push_back( 40 );
L.push_back( 20 );
L.push_back( 10 );
L.push_back( 30 );
L.push_back( 0 );

cout << "L = ( " ;
for ( Iter = L.begin( ) ; Iter != L.end( ) ; Iter++ )
co…
[/Quote]
改写了一下:

#include <list>
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
list <int> L;
list <int>::iterator Iter;
/*
L.push_back( 40 );
L.push_back( 20 );
L.push_back( 10 );
L.push_back( 30 );
L.push_back( 0 );
*/
int x;
while (cin >> x)
L.push_back(x);

cout << "L = ( " ;
for (Iter = L.begin() ; Iter != L.end() ; Iter++)
cout << *Iter << " ";
cout << ")" << endl;

list <int>::reverse_iterator result;
result = find(L.rbegin(), L.rend(), 0);
if (result == L.rend())
cout << "There is no 0 in list L.";
else
{
cout << "There is a 0 in list L";

if (result != L.rbegin()) // 输出边上的一个值
{
cout << " , and it is surround by " << *--result;
++result;
}
if (++result != L.rend()) // 输出边上的另一个值
{
cout << " and " << *result << endl;
}
}
cout << endl;

return 0;
}
ysysbaobei 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hqin6 的回复:]
一个while循环?为啥不用find_end呢?
[/Quote]
用 find_end()写了一个

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
int x;
list<int> ilist;
while (cin >> x)
ilist.push_back(x);
list<int>::reverse_iterator rlter;
int search(0);
rlter = find(ilist.rbegin(), ilist.rend(), search); // p281/295
cout << *rlter << endl;
cout << *--rlter << endl; // 测试是否是最后一个0而用的;-- 指向 顺序方向的下一个元素

list<int> ilist2(1);
list<int>::iterator lter = find_end(ilist.begin(), ilist.end(), ilist2.begin(), ilist2.end()); // find_end()查找
cout << *lter << endl;
cout << *++lter << endl;

return 0;
}
qq675927952 2008-11-20
  • 打赏
  • 举报
回复
up

65,211

社区成员

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

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