Форум » C/C++ для начинающих (C/C++ for beginners) » combinations find in 1d array » Ответить

combinations find in 1d array

ramees: how can i display only same value starting and ending groups in a 1d array this is the example array int a[12]={2,1,1,2,2,4,5,4,5,4,5,4}; i need to find all combination in this array output ......... 2,1,1,2 4,5,4 5,4,5 4,5,4 5,4,5 4,5,4

Ответов - 5

Сыроежка: Why are there absent combinations 1 1 and 2 2?

ramees: for example this is a combination 4 5 4 a combination need its first value and last value matching and also minimum three value below combinations i don't want to pick 1 1 1 1 2 5 1 1 1

Сыроежка: I have not understood why this combination 1 2 5 1 does not satisfy the condition? Is it because 2 and 5 are unequal each other?


Сыроежка: If I have understood correctly what you need is the following [pre2] #include <iostream> #include <algorithm> #include <iterator> #include <functional> template <typename ForwardIterator> void display_ranges( ForwardIterator first, ForwardIterator last, std::ostream &os = std::cout ) { typedef typename std::iterator_traits<ForwardIterator>::value_type value_type; ForwardIterator next; first = std::adjacent_find( first, last, std::not_equal_to<value_type>() ); if ( first != last ) { while ( ( next = std::adjacent_find( std::next( first ), last, std::not_equal_to<value_type>() ) ) != last ) { if ( *first == *std::next( next ) ) { std::copy( first, std::next( next, 2 ), std::ostream_iterator<value_type>( os, " " ) ); os << std::endl; } first = next; } } } int main() { int a[] = { 2, 1, 1, 2, 2, 4, 5, 4, 5, 4, 5, 4 }; display_ranges( std::begin( a ), std::end( a ) ); return 0; } [/pre2] The output will be [pre2] 2 1 1 2 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 [/pre2]

ramees: yes exactly what i needed thank's



полная версия страницы