por que std :: search precisa encaminhar iters
my issue é idêntico ao tópico abaixo, estou lutando para entender as respostas fornecidas, ou melhor, meu código não deve funcionar, pois usa apenas iteradores de entrada ..mas minha função parece funcionar e se comportar de forma idêntica ao std :: search ..por isso estou perplexo e detesto seguir em frente sem entender corretamente ... talvez se alguém puder sugerir uma entrada que interrompa minha função, mas não o std ::
A partir dePor que preciso de um Iterador Direto para implementar meu std personalizado :: search :
Estudo do livro "C ++ Acelerado" da Koenig & Mo
Exercício 8-2 solicita que eu implemente por conta própria algumas funções padronizadas de <algorithm> e <numeric>, e especifique que tipo de iterador minha implementação reque
Ao tentar implementar std :: search, determinei que só precisava de iteradores de "entrada"
No entanto, olhando para a implementação do std :: search instalado com meu compilador, posso ver que eles usam iteradores "forward", mas não consigo entender o porquê, porque não há necessidade de escrever, apenas ler e inserir iteradores. o requerimento
Alguém aqui pode me ajudar a entender isso, por favor? Por que eu precisaria usar iteradores "forward" para implementar std :: search?
Desde já, obrigado
myfunction:
template <class In>
In search( In begin, In end, In begin2, In end2 )
{
In found ; // iter: 1st element in pattern match(in content)
In pattern_begin = begin2 ; // iter: 1st element in search pattern.
int flag = 0 ; // flag: partial match found?
// search content for pattern
while ( begin < end ) {
// if pattern-match fails ..reset vars
// & continue searching remaining content/elements
if ( *begin != *begin2 ) {
In ret ;
begin2 = pattern_begin ;
flag = 0 ;
begin++ ;
} else {
// compare next element in pattern with next element in content.
// if: 1st element of 'pattern' is found, store iter to it's pos
// ..then if entire pattern is found, we can ret an iter to where it starts
if ( flag == 0 ) {
found = begin ;
flag = 1 ;
}
// inc iters to compare next elements in partial match
begin++ ;
begin2++ ;
}
// if: iter is 1-past end of search pattern
// then entire pattern has been found
// return the iter to where it starts
if( begin2 == end2 ) { return found ; }
}
// end of content reached, no complete pattern found
// begin should? equal an iter 1-past the end of content
return begin ;
}
motorista
///* // Driver: custom::search( b, e, b2, e2 )
#include <string>
#include <vector>
#include <iostream>
//#include <algorithm>
#include "library_algorithms.h"
int main() {
// init string test
std::string content = "fo The fox foxu jumped foxe foxy " ;
std::string search_pattern = "foxy" ;
// func test on string
std::string::iterator ret_iter =
custom::search( content.begin(), content.end(), search_pattern.begin(), search_pattern.end() ) ;
//std::search( content.begin(), content.end(), search_pattern.begin(), search_pattern.end() ) ;
// output
if ( ret_iter != content.end() ) {
std::cout << std::endl << std::endl << search_pattern << ": found at position: " << int( ret_iter - content.begin() ) << std::endl;
} else {
std::cout << std::endl << std::endl << search_pattern << ": ...not found" << std::endl;
}
// Init vec test:
// create content values in range: 10 20 30 <......> 9970 9980 9990
std::vector<int> myvector;
for (int i=1; i<1000; i++) myvector.push_back(i*10);
// create pattern values to search for
std::vector<int> pattern ;
pattern.push_back( 3730 ) ;
pattern.push_back( 3740 ) ;
pattern.push_back( 3750 ) ;
pattern.push_back( 3760 ) ;
// test: func on vector<int>
std::vector<int>::iterator it;
it = custom::search ( myvector.begin(), myvector.end(), pattern.begin(), pattern.end() );
// output
if (it!=myvector.end())
std::cout << std::endl << std::endl << "pattern found at position " << int(it-myvector.begin()) << std::endl;
else
std::cout << std::endl << std::endl << "pattern not found" << std::endl;
return 0 ;
}