如何返回值的问题

lily604 2008-03-30 11:41:40
 
返回值 sub_set(int M,int N) 

    int *L=new int[N], i;
for( i=0; i<N; ++i )
L[i]=(i>=N-M)? 1 : 0;
do
copy(L,L+N,ostream_iterator<int>(cout)),cout<<endl;
while(next_permutation(L,L+N));
delete []L;
  }
如果有这样一个函数,我怎样才能返回cout所打印的所有值.
这个ostream_iterator<int>(cout))是什么意思啊?
...全文
103 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
HelloDan 2008-03-31
  • 打赏
  • 举报
回复
上面的STL我也不是太熟悉。
HelloDan 2008-03-31
  • 打赏
  • 举报
回复
An ostream_iterator is an Output Iterator that performs formatted output of objects of type T to a particular ostream. Note that all of the restrictions of an Output Iterator must be obeyed, including the restrictions on the ordering of operator* and operator++ operations.
qmm161 2008-03-31
  • 打赏
  • 举报
回复
用一个vector,每次循环重排后,将数组L中的内容push_back,然后返回!
后面那个问题是流迭代器!
Chappell 2008-03-31
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>

using namespace std ;

void sub_set(int M,int N,vector<string>& vec)
{
typedef vector<char> StrVector ;
typedef StrVector::iterator StrVectorIt ;
typedef ostream_iterator<char> StrOstreamIt;

StrVector Pattern(M+N);

StrVectorIt start, end, it ;

StrOstreamIt outIt(cout, " ") ;

start = Pattern.begin() ; // location of first
// element of Pattern

end = Pattern.end() ; // one past the location last
// element of Pattern
for(int i=0;i<M+N;i++)
{
Pattern[i]=i<N?'0':'1';
}
cout << "Before calling next_permutation...\n" << "Pattern: " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << "\n\n" ;

cout << "After calling next_permutation...." << endl ;
do
{
string str(start,end);
vec.push_back(str);
}while ( next_permutation(start, end));
}

//保存到vec中
void main()
{
vector<string> vec;
sub_set(5,3,vec);
copy(vec.begin(),vec.end(),ostream_iterator<string>(cout,"\n"));

}

lily604 2008-03-31
  • 打赏
  • 举报
回复
按照三楼的办法,我确实返回了int *,在主函数也能收到,但是sub_set(int M,int N)函数仍然向屏幕输出01,我不想让它向屏幕输出该怎么做啊
liyuzhu_1984 2008-03-31
  • 打赏
  • 举报
回复
我觉得最好做一个全局变量来处理

如果不delete[] 那么很可能造成内存泄露

ostream_iterator <int>(cout)) 到像个遍历器的作用
xbt746 2008-03-31
  • 打赏
  • 举报
回复
查查STL,有详细说明
hityct1 2008-03-31
  • 打赏
  • 举报
回复
如5楼所说。
ostream_iterator <int>(cout))

cout是与标准输出相关联的,这样迭代器的内容直接回打印到屏幕了。
Soulic 2008-03-31
  • 打赏
  • 举报
回复
不太明白,学习了
ryfdizuo 2008-03-31
  • 打赏
  • 举报
回复
ostream_iterator <int>(cout))
用cout去初始化一个ostream_iterator<int>对象;
你可以自己写一个函数将cout内容保存,函数返回嘛,
Skt32 2008-03-31
  • 打赏
  • 举报
回复
找本STl书看看
野男孩 2008-03-31
  • 打赏
  • 举报
回复
没太明白lz的意思。。。

感觉是不是最后不要delete[] L,直接return L;就行了?sub_set(int M,int N)改成int* sub_set(int M,int N) 

64,649

社区成员

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

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