范围计数 [英] range count

查看:88
本文介绍了范围计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个矢量< charwhich看起来像这样(adddadsgedddd

dk)

我需要得到连续''d'的最大数量。 5在这个例子中


我正在使用这种方法,但不确定它是否是最佳的。


感谢


int k = 0;

vector :: const_iterator i = find(v.begin(),v.end(),''r'')

while(i!= v.end()){

vector :: const_iterator j = find(i,v.end(),any_thing_but''r'')

if((j - i)k)k =(j - i)

i = j

}


Hi
I have a vector<charwhich looks like this (a d d d a d s g e d d d d
d k)
I need to get the biggest count of consecutive ''d''. 5 in this example

I am toying with this method but not sure if it is optimal.

thanks

int k = 0;
vector::const_iterator i = find(v.begin(), v.end(), ''r'')
while ( i != v.end() ) {
vector::const_iterator j = find(i, v.end(), any_thing_but ''r'')
if ( (j - i) k ) k = (j - i)
i = j
}

推荐答案

在文章< 87 ************ @ localhost.localdomain>,

Gary Wessle< ph * ***@yahoo.comwrote:
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.comwrote:



我有一个向量< charwhich看起来像这样(adddadsgedddd

dk)

我需要获得连续d的最大数量。 5在这个例子中


我正在使用这种方法,但不确定它是否是最佳的。


感谢


int k = 0;

vector :: const_iterator i = find(v.begin(),v.end(),''r'')

while(i!= v.end()){

vector :: const_iterator j = find(i,v.end(),any_thing_but''r'')

if((j - i)k)k =(j - i)

i = j

}
Hi
I have a vector<charwhich looks like this (a d d d a d s g e d d d d
d k)
I need to get the biggest count of consecutive ''d''. 5 in this example

I am toying with this method but not sure if it is optimal.

thanks

int k = 0;
vector::const_iterator i = find(v.begin(), v.end(), ''r'')
while ( i != v.end() ) {
vector::const_iterator j = find(i, v.end(), any_thing_but ''r'')
if ( (j - i) k ) k = (j - i)
i = j
}



我会假设''r'是一个错字。难道不是最后一行是什么

喜欢i = find(j,v.end(),''r'');"?


这可能是for_each的有用时间。 (未编译的,未经测试的代码)


struct count_consecutive:unary_function< char,void>

{

char comp;

unsigned count;

unsigned max_count;

count_same(char值)

:comp(值)

,count(0)

,max_count(0)

{}

void operator()(char c){

if(c == comp)

+ + count;

else {

max_count = max(count,max_count);

count = 0;

}

}


unsigned result()const {return max(count,max_count); }

}


k = for_each(v.begin(),v.end(),count_consecutive(''d''))。result() ;


上面的好处是它可以在初始化程序中使用。

I''ll assume the ''r'' was a typo. Shouldn''t that last line be something
like "i = find( j, v.end(), ''r'' );"?

This might be a useful time for for_each. (uncompiled, untested code)

struct count_consecutive: unary_function< char, void >
{
char comp;
unsigned count;
unsigned max_count;
count_same( char value )
: comp( value )
, count( 0 )
, max_count( 0 )
{ }
void operator()( char c ) {
if ( c == comp )
++count;
else {
max_count = max( count, max_count );
count = 0;
}
}

unsigned result() const { return max( count, max_count ); }
}

k = for_each( v.begin(), v.end(), count_consecutive( ''d'' ) ).result();

The nice thing about the above is that it can be used in an initializer.


Daniel T. ; < da ****** @ earthlink.netwrites:
"Daniel T." <da******@earthlink.netwrites:

文章< 87 ************ @ localhost。 localdomain>,

Gary Wessle< ph **** @ yahoo.comwrote:
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.comwrote:



我有一个矢量< charwhich看起来像这样(adddadsgedddd

dk)

我需要得到连续''d'的最大数量。 5在这个例子中


我正在使用这种方法,但不确定它是否是最佳的。


感谢


int k = 0;

vector :: const_iterator i = find(v.begin(),v.end(),''r'')

while(i!= v.end()){

vector :: const_iterator j = find(i,v.end(),any_thing_but''r'')

if((j - i)k)k =(j - i)

i = j

}
Hi
I have a vector<charwhich looks like this (a d d d a d s g e d d d d
d k)
I need to get the biggest count of consecutive ''d''. 5 in this example

I am toying with this method but not sure if it is optimal.

thanks

int k = 0;
vector::const_iterator i = find(v.begin(), v.end(), ''r'')
while ( i != v.end() ) {
vector::const_iterator j = find(i, v.end(), any_thing_but ''r'')
if ( (j - i) k ) k = (j - i)
i = j
}



我会假设''r'是一个错字。难道不是最后一行是什么

喜欢i = find(j,v.end(),''r'');"?


这可能是for_each的有用时间。 (未编译的,未经测试的代码)


struct count_consecutive:unary_function< char,void>

{

char comp;

unsigned count;

unsigned max_count;

count_same(char值)

:comp(值)

,count(0)

,max_count(0)

{}

void operator()(char c){

if(c == comp)

+ + count;

else {

max_count = max(count,max_count);

count = 0;

}

}


unsigned result()const {return max(count,max_count); }

}


k = for_each(v.begin(),v.end(),count_consecutive(''d''))。result() ;


上面的好处是它可以在初始化器中使用。


I''ll assume the ''r'' was a typo. Shouldn''t that last line be something
like "i = find( j, v.end(), ''r'' );"?

This might be a useful time for for_each. (uncompiled, untested code)

struct count_consecutive: unary_function< char, void >
{
char comp;
unsigned count;
unsigned max_count;
count_same( char value )
: comp( value )
, count( 0 )
, max_count( 0 )
{ }
void operator()( char c ) {
if ( c == comp )
++count;
else {
max_count = max( count, max_count );
count = 0;
}
}

unsigned result() const { return max( count, max_count ); }
}

k = for_each( v.begin(), v.end(), count_consecutive( ''d'' ) ).result();

The nice thing about the above is that it can be used in an initializer.



感谢你花时间发布这个,:)我很难

了解你给我的新代码状态。我宁愿

写一些我现在可以掌握它的东西,直到我的水平上升。


这里是我最好的关闭


**************** statistics.h ****************

# ifndef STATISTICS_H

#define STATISTICS_H

#include< vector>

使用std :: vector;


#include< map>

使用std :: map;


班级统计

{

map< char,intmp;

vector< charv;

void stock_take();

int bigest_r(vector< charconst& cv);

bool not_r(char c);


public:

statistics(vector< charconst& cv);

~re statistics();

map< char,intget_counts;

};


#endif

**************** statistics.cpp ****************

#include< vector>

#include" statistics.h"


using namespace std;


统计::统计(矢量< charconst&安培; cv)

:v(cv)

{

statistics :: stock_take();

}


void statistics :: stock_take()

{

mp [''c''] = count(v.begin() ,v.end(),''c'');

mp [''r''] = bigest_r(v);

}


bool statistics :: not_r(char c)

{

return(c!=''r'');

}


int statistics :: bigest_r(vector< charconst& cv)

{

int k = 0;

vector_iterator i = find(cv.begin(),v.end(),''r'');

while(i!= cv.end() ){

vector_iterator j = find_if(i,cv.end(),not_r);

if((ji)k)k =(ji);

i = j;

}

返回k;

}

};



**************** statistics_test.cpp ****************

#include< iostream>

使用std :: cout;


#include< vector>

使用std :: vector;


#include< utility>

使用std :: map;


#include" statistics.h" ;


int main(){


vector v = {crrrdxccddddk};

statistics st1(v) ;

cout<< " C" << (st1.get_counts)[c]

<< " R" << (st1.get_counts)[r]

<< ''\ n'';

}

thank you for taking the time to post this, :) I am having hard time
understanding the code you gave because of my newbi status. I''d rather
write something I can grasp it for now till my level goes up.

here is my best shut

**************** statistics.h ****************
#ifndef STATISTICS_H
#define STATISTICS_H
#include <vector>
using std::vector;

#include <map>
using std::map;

class statistics
{
map<char, intmp;
vector<charv;
void stock_take();
int bigest_r(vector<charconst& cv);
bool not_r(char c);

public:
statistics(vector<charconst& cv);
~statistics();
map<char,intget_counts;
};

#endif
**************** statistics.cpp ****************
#include <vector>
#include "statistics.h"

using namespace std;

statistics::statistics(vector<charconst& cv)
: v( cv )
{
statistics::stock_take();
}

void statistics::stock_take()
{
mp[''c''] = count(v.begin(), v.end(), ''c'');
mp[''r''] = bigest_r(v);
}

bool statistics::not_r(char c)
{
return (c != ''r'');
}

int statistics::bigest_r(vector<charconst& cv)
{
int k = 0;
vector_iterator i = find(cv.begin(), v.end(), ''r'');
while (i != cv.end()){
vector_iterator j = find_if(i, cv.end(), not_r);
if ( (j-i) k ) k = (j-i);
i = j;
}
return k;
}
};


**************** statistics_test.cpp ****************
#include <iostream>
using std::cout;

#include <vector>
using std::vector;

#include <utility>
using std::map;

#include "statistics.h"

int main() {

vector v = {c r r r d x c c d d d d k};
statistics st1(v);
cout << "c" << (st1.get_counts)[c]
<< "r" << (st1.get_counts)[r]
<< ''\n'';
}


文章< 87 ************ @ localhost.localdomain>,

Gary Wessle< ph **** @ yahoo.comwrote:
In article <87************@localhost.localdomain>,
Gary Wessle <ph****@yahoo.comwrote:

" Daniel T." < da ****** @ earthlink.netwrites:
"Daniel T." <da******@earthlink.netwrites:

这可能是for_each的有用时间。
This might be a useful time for for_each.



首先,让我清理一下我写的内容:

struct count_consecutive:unary_function< char,void>

{

char comp;

unsigned count;

unsigned max_count;


count_consecutive(char值)

:comp(值)

,count(0)

,max_count (0)

{}


void operator()(char c){

if(c == comp)< br $>
++ count;

else {

max_count = max(count,max_count);

count = 0;

}

}


unsigned result()const {return max(count,max_count); }

};


k = for_each(v.begin(),v.end(),count_consecutive(''d''))。result( );

First, let me clean up what I wrote:

struct count_consecutive: unary_function< char, void >
{
char comp;
unsigned count;
unsigned max_count;

count_consecutive( char value )
: comp( value )
, count( 0 )
, max_count( 0 )
{ }

void operator()( char c ) {
if ( c == comp )
++count;
else {
max_count = max( count, max_count );
count = 0;
}
}

unsigned result() const { return max( count, max_count ); }
};

k = for_each( v.begin(), v.end(), count_consecutive( ''d'' ) ).result();


感谢你花时间发布这个,:)我很难

理解你给的代码因为我的新生态。我宁愿

写一些我现在可以掌握的东西,直到我的水平上升。
thank you for taking the time to post this, :) I am having hard time
understanding the code you gave because of my newbi status. I''d rather
write something I can grasp it for now till my level goes up.



这没关系。让我们来看看你写的算法:

That''s fine. Let''s look at the algorithm you wrote:


int statistics :: bigest_r(vector< charconst& cv)

{

int k = 0;

vector_iterator i = find(cv.begin(),v.end(),''r'');

while(i!= cv.end()){

vector_iterator j = find_if(i,cv.end(),not_r);

if((ji) k)k =(ji);

i = j;

}

返回k;

}
int statistics::bigest_r(vector<charconst& cv)
{
int k = 0;
vector_iterator i = find(cv.begin(), v.end(), ''r'');
while (i != cv.end()){
vector_iterator j = find_if(i, cv.end(), not_r);
if ( (j-i) k ) k = (j-i);
i = j;
}
return k;
}



显然你的代码没有编译,但我现在要忽略它为

现在看看你的算法:


arrrarsgerrrrrk


你的算法会对上面的字符做什么?


find将返回元素1(数组中的第二个元素,然后

find_if将返回元素4.


4 - 1 0所以k被赋值3.
< br / >
现在我将设置为指向元素4,然后我们循环回来。


这不是向量的结尾,所以find_if将设置j指向

元素4(第一个元素是not_r。)


4 - 4 3是假的,所以循环备份(用i和j两个指向

元素4仍然。)


你陷入无限循环。


你看, " I"是你''r'和'j'的连续点的开始。是结束的结束

,但是你指定了i。到了
''r'的第一个连续结束,并期望事情仍然有效。在你的while块结束时,

你需要重置i到'r'的下一个连续开头(提示,

再次使用find。)


关于语法。 ..你需要定义vector_iterator的类型。和

" not_r"必须是全局函数,而不是成员函数。 (你可以

使它成为一个成员函数,但它会更复杂,而且b / b
毫无意义。

Obviously your code doesn''t compile, but I''m going to ignore that for
now and look at your algorithm:

a r r r a r s g e r r r r r k

what will your algorithm do with the chars above?

find will return element 1 (the second element in the array,) then
find_if will return element 4.

4 - 1 0 so k is assigned 3.

Now i will be set to point at element 4, and we loop back up.

That''s not the end of the vector, so find_if will set j to point at
element 4 (the first element that is not_r.)

4 - 4 3 is false, so loop back up (with i and j both pointing at
element 4 still.)

Your stuck in an infinite loop.

You see, "i" is the beginning of your streak of ''r''s, and "j" is the end
of the streak, but then you assign "i" to the end of the first streak of
''r''s and expect things to still work. At the end of your while block,
you need to reset "i" to the beginning of the next streak of ''r''s (hint,
use "find" again.)

As to the syntax... You need to define the type of "vector_iterator" and
"not_r" must be a global function, not a member function. (You could
make it a member function but it would be much more complicated and
pointless.


这篇关于范围计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆