求数组下标问题

pangder 2008-05-11 11:20:51
有数组int a[]={1,2,3,4,5,6,7,8},查找4的位置,把a复制给b,将a的内容反转。再次查找4的位置。我不知道怎么查找出4的位置,谁能教教我~谢谢
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
void main(){
int a[]={1,2,3,4,5,6,7,8},b[8];
copy(a,a+8,b);
reverse(a,a+8);
cout<<( *find(a,a+8,4)==4)<<endl;
copy(a,a+8,ostream_iterator<int>(cout," "));
cout<<endl;
copy(b,b+8,ostream_iterator<int>(cout," "));
cout<<endl;
}
...全文
103 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
visame 2008-05-12
  • 打赏
  • 举报
回复

在我的G++下运行正常
1 #include <iterator>
2 #include <iostream>
3 #include <algorithm>
4 #include <functional>
5 using namespace std;
6 int main()
7 {
8 int a[]={1,2,3,4,5,6,7,8},b[8];
9 copy(a,a+8,b);
10 reverse(a,a+8);
11 cout <<( *find(a,a+8,4)==4) <<endl;
12 copy(a,a+8,ostream_iterator <int>(cout," "));
13 cout <<endl;
14 copy(b,b+8,ostream_iterator <int>(cout," "));
15 cout <<endl;
16 }
OUTPUT:
1
8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8
Treazy 2008-05-12
  • 打赏
  • 举报
回复
char *strchr(char *str,char ch)

在字符串str中搜索一个字符ch的位置
pangder 2008-05-12
  • 打赏
  • 举报
回复
终于弄明白了~谢谢goodallen1985 和HelloDan
结贴!!!
qmm161 2008-05-12
  • 打赏
  • 举报
回复
这种代码还是不要用stl来写吧!如果不知道怎么查找4,即便告诉你有这个库函数可以用,也没多大意义!
goodallen1985 2008-05-12
  • 打赏
  • 举报
回复
晕死,粘贴错了,两次!
#include <iostream> 
#include <algorithm>
#include <functional>
using namespace std;

void main()
{

int a[]={1,2,3,4,5,6,7,8},b[8];
copy(a,a+8,b);
reverse(a,a+8);

cout <<(find(a,a+8,4)-a)<<endl;
copy(a,a+8,ostream_iterator <int>(cout," "));
cout <<endl;
cout <<(find(b,b+8,4)-b)<<endl;
copy(b,b+8,ostream_iterator <int>(cout," "));
cout <<endl;

}
goodallen1985 2008-05-12
  • 打赏
  • 举报
回复

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

void main()
{
int a[]={1,2,3,4,5,6,7,8},b[8];
copy(a,a+8,b);
reverse(a,a+8);

cout <<( *find(a,a+8,4))<<endl;
copy(a,a+8,ostream_iterator <int>(cout," "));
cout <<endl;
cout <<( *find(b,b+8,4))<<endl;
copy(b,b+8,ostream_iterator <int>(cout," "));
cout <<endl;
}
goodallen1985 2008-05-12
  • 打赏
  • 举报
回复

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

void main()
{
int a[]={1,2,3,4,5,6,7,8},b[8];
copy(a,a+8,b);
reverse(a,a+8);

cout <<( *find(a,a+8,4))<<endl;
copy(a,a+8,ostream_iterator <int>(cout," "));
cout <<endl;
cout <<( *find(b,b+8,4))<<endl;
copy(b,b+8,ostream_iterator <int>(cout," "));
cout <<endl;
}
pangder 2008-05-12
  • 打赏
  • 举报
回复
2楼的朋友,运行你的程序在FOR那出错。
我想问下cout <<( find(a,a+8,4)-a) <<endl这个语句是什么意思
HelloDan 2008-05-11
  • 打赏
  • 举报
回复

#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

int main()
{
int a[]={1,2,3,4,5,6,7,8},b[8];
copy(a,a+8,b);
reverse(a,a+8);
cout <<( find(a,a+8,4)-a) <<endl;
//copy(a,a+8,ostream_iterator <int>(cout," "));
for(int i=0;i<8;++i)
cout<<a[i]<<" ";
cout <<endl;
//copy(b,b+8,ostream_iterator <int>(cout," "));
for(int i=0;i<8;++i)
cout<<b[i]<<" ";
cout <<endl;
cout <<endl;

return 0;
}
//不好意思,那个ostream_iterator的好久没有用过,忘了。
iu_81 2008-05-11
  • 打赏
  • 举报
回复
strchr

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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