On-line: гостей 1. Всего: 1 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 23.05.14 18:24. Заголовок: How to Group a value inside the Array based on high value and low value


i have a random number dynamic array in my code i need to filter my array. See below: link
http://tinypic.com/view.php?pic=28iwzz9&s=8#.U3881HL7Jt8

int ary[]={2 ,5,6,7,5,4,2,1 , 3 ,3,6,10,9,6,2,1 };

i need output like this : out 1: 2 , 5 , 6 , 7 , 5 , 4 , 2 , 1
out 2: 3 , 3 , 6 ,10, 9 , 6 , 2 , 1

if my array like int a[] = { 1, 2, 1, 1, 1, 3 };

out 1: 1, 2, 1, 1, 1
out 2: 3,

if my array like int a[] = { 1, 2, 1, 2, 1, 3 };

out 1: 1, 2, 1,
out 2: 2, 1,
out 3: 3,

first largest value and its surrounding value group together and so on......



Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 21 , стр: 1 2 All [только новые]





ссылка на сообщение  Отправлено: 23.05.14 18:39. Заголовок: Well, You can use st..


Well, You can use standard algorithm std::adjacent_find declared in header <algorithm>. For example


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

int main()
{
int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 };

int *p = std::begin( a );

while ( p != std::end( a ) )
{
int *q = std::adjacent_find( p, std::end( a ), std::greater<int>() );
q = std::adjacent_find( q, std::end( a ), std::less<int>() );
if ( q != std::end( a ) ) ++q;
std::copy( p, q,
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
p = q;
}

return 0;
}


The output is

 
2 5 6 7 5 4 2 1
3 3 6 10 9 6 2 1


You can check the code using the on-line compiler at www.ideone.com.
However take into account that in the original example at www.stackoverflow you showed another logic of splitting an array when there are adjacent equal elements.

The other way to do the task is to use standard algorithm std::is_sorted_until.



Спасибо: 1 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 24.05.14 18:26. Заголовок: add output into dynamic array


thanks

how can i add this outputs : out 1 : 2 5 6 7 5 4 2 1
out 2 : 3 3 6 10 9 6 2 1
into a dynamic array i don't no how this section works
std::copy( p, q,
std::ostream_iterator<int>( std::cout, " " ) );



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 25.05.14 13:49. Заголовок: Construction std::..


Construction
 
std::copy( p, q,
std::ostream_iterator<int>( std::cout, " " ) );


is a call of standard algorithm std::copy that copies elements in the range [p, q) into standard output stream. In fact

 
std::ostream_iterator<int>( std::cout, " " )

is equivalent to
 
std::cout << *p << " ";

for each p that preceeds to q.

You could write instead for example
 
for ( auto t = p; t != q; ++t ) std::cout << *t << " ";

As for your second question then it is better and simpler to use standard container std::vector instead of using dynamically allocated arrays.

For example


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

int main()
{
int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 };

int *p = std::begin( a );

std::vector<std::vector<int>> v;

while ( p != std::end( a ) )
{
int *q = std::adjacent_find( p, std::end( a ), std::greater<int>() );
q = std::adjacent_find( q, std::end( a ), std::less<int>() );
if ( q != std::end( a ) ) ++q;

v.push_back( std::vector<int>() );
v.back().reserve( std::distance( p, q ) );

std::copy( p, q,
std::back_inserter( v.back() ) );
p = q;
}

for ( const std::vector<int> &v1 : v )
{
std::copy( v1.begin(), v1.end(),
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
}

return 0;
}


If to use dynamically allocated arrays then you should at first count how many there are subsequences in the original sequence. It means that the while loop has to be executed twice.


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 26.05.14 17:48. Заголовок: thanks


yes this will work

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 27.05.14 09:48. Заголовок: I am sorry. The code..


I am sorry. The code could be written simpler

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

int main()
{
int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 };

auto first = std::begin( a );

std::vector<std::vector<int>> v;

while ( first != std::end( a ) )
{
auto last = std::adjacent_find( first, std::end( a ), std::greater<int>() );
last = std::adjacent_find( last, std::end( a ), std::less<int>() );
if ( last != std::end( a ) ) ++last;

v.push_back( std::vector<int>( first, last ) );
first = last;
}

for ( const std::vector<int> &v1 : v )
{
std::copy( v1.begin(), v1.end(),
std::ostream_iterator<int>( std::cout, " " ) );
std::cout << std::endl;
}

return 0;
}



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 27.05.14 16:13. Заголовок: how about this one ..


how about this one

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

using namespace std;
const int X = 1000;
int main()
{

int *AryR;
AryR = new int [X];

int cu = 0;

int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 };

int *p = std::begin( a );

while ( p != std::end( a ) )
{
int *q = std::adjacent_find( p, std::end( a ), std::greater<int>() );
q = std::adjacent_find( q, std::end( a ), std::less<int>() );
if ( q != std::end( a ) ) ++q;

for ( auto t = p; t != q; ++t ) { // std::cout << *t << " "

AryR[cu]=*t;
cu++;
p = q;
}

for(int i =0; i<cu;i++ ){

cout<<AryR[ i ];
}

cout<<" "<<endl;
cu = 0;
}

return 0;
}

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 27.05.14 21:10. Заголовок: It is a bad code.:) ..


It is a bad code.:)

First of all it is not clear why the dynamically allocated array has 1000 elements. Secondly you overwrite each time the previously extracted subsequence.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 13:02. Заголовок: ok then i will count..


ok then i will countine with vector

i put this code into nuke plugin code its give me this error . no instance of overloaded function "std::adjacent_find" matches the argument list

for screen capture imge pls see below link

click here

click here


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 13:23. Заголовок: Please show error me..


Please show error messages here. Do not use links because images are loaded slowly.

In any case the error messages mean that you incorrectly specified arguments of the algorithm. See my example above and do the same way.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 16:19. Заголовок: red struggle under..


red struggle under line in all std:: when i hover over mouse this error pop up : no instance of overloaded function "std::adjacent_find" matches the argument list
in this code i already using DD::image namespace i think this is some name space error i don't no how to tell so
i took a print screen image for better understanding

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 28.05.14 20:20. Заголовок: I showed you code. ..


I showed you code. So you should use it as a template. If some errors occur then they are errors of your code. The code I showed is compiled without any error. I do not know what is DD::image namespace. It has nothing common with the code I showed.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.05.14 09:24. Заголовок: is there any way to ..


is there any way to do this code without std algorithm

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 31.05.14 13:36. Заголовок: Any standard algorit..


Any standard algorithm is a regular function. So you can write yourself the corresponding function. For example this call

 
std::adjacent_find( first, std::end( a ), std::greater<int>() );


can be substituted for a call of the following function

 
const int * find_greater( const int a[], size_t n )
{
if ( n != 0 )
{
for ( const int *prev = a, *next = a; ++next != a + n; ++prev )
{
if ( *next < *prev ) return prev;
}
}

return ( a + n );
}


The program can be wriiten as

 
#include <iostream>
#include <vector>

const int * find_greater( const int a[], size_t n )
{
if ( n != 0 )
{
for ( const int *prev = a, *next = a; ++next != a + n; ++prev )
{
if ( *next < *prev ) return prev;
}
}

return ( a + n );
}

const int * find_less( const int a[], size_t n )
{
if ( n != 0 )
{
for ( const int *prev = a, *next = a; ++next != a + n; ++prev )
{
if ( *prev < *next ) return prev;
}
}

return ( a + n );
}

int main()
{
int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 };
size_t n = sizeof( a ) / sizeof( *a );

const int *first = a;

std::vector<std::vector<int>> v;
while ( first != a + n )
{
const int * last = find_greater( first, n - ( first - a ) );
last = find_less( last, n - ( last - a ) );

if ( last != a + n ) ++last;

v.push_back( std::vector<int>( first, last ) );
first = last;
}

for ( const std::vector<int> &v1 : v )
{
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
}

return 0;
}



The output is

 
2 5 6 7 5 4 2 1
3 3 6 10 9 6 2 1


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 01.06.14 09:21. Заголовок: intelsense error ..


intelsense error because i am using visual studio 10 c++ is not fully support c++11 features i try to compile both version in ideone. older cpp error in this section below :in visual studio 10 Error red stuggle in " : " when i hover over mouse this error pop up -> referance variable "v1" requires an initializer

for ( const std::vector<int> &v1 : v )
{
for ( int x : v1 ) std::cout << x << ' ';
std::cout << std::endl;
}

i can't use new version visual studio how can i do this in older c++


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 01.06.14 12:55. Заголовок: MS VS 2010 has its o..


MS VS 2010 has its own language extension for the range-based for statement. You may use the following construction

 
for each ( const std::vector<int> &v1 in v )
{
for each ( int x in v1 ) std::cout << x << ' ';
std::cout << std::endl;
}


Or you can use regular for statements either with an index or with an iterator. For example

 
for ( std::vector<std::vector<int>>::size_type i = 0; i < v.size(); i++ )
{
for ( std::vector<int>::size_type j = 0; j < v.size(); j++ )
{
std::cout << v[ i ][ j ] << ' ';
}
std::cout << std::endl;
}

for ( std::vector<std::vector<int>>::iterator it1 = v.begin(); it1 != v.end(); ++it1 )
{
for ( std::vector<int>::iterator it2 = it1->begin(); it2 != it1->end(); ++it2 )
{
std::cout << *it2 << ' ';
}
std::cout << std::endl;
}




Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 01.06.14 13:53. Заголовок: i seen a lot for eac..


i seen a lot for each in my code i don't no how its work and what is the difference between foreach & for loop

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 01.06.14 14:13. Заголовок: It simply sorts out ..


It simply sorts out elements in a container. For example

 
#include <iostream>

int main()
{
int a[] = { 1, 2, 3, 4, 5 };
for each ( int x in a ) std::cout << x << ' ';
std::cout << std::endl;
}



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.06.14 08:50. Заголовок: i try to change arra..


i try to change array to vector but error
how to do this in a vector

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 05.06.14 18:25. Заголовок: I do not understand ..


I do not understand what array you are speaking about. In this thread there is shown enough code that demonstrates how to work with class std::vector. and how to do the task you asked about.

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 06.06.14 08:40. Заголовок: int a = { 2, 5, 6,..



int a[] = { 2, 5, 6, 7, 5, 4, 2, 1, 3, 3, 6, 10, 9, 6, 2, 1 }; in ur code we get data from array. i like to get data from

vector<int> a;

i try to change but error.

sorry for asking same question again an again i have no other option to get right answers i learning c++ from youtube so i don't have any professor
to ask .

Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 21 , стр: 1 2 All [только новые]
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 17
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет