• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Zadanie, szablon czy nie?

Object Storage Arubacloud
0 głosów
270 wizyt
pytanie zadane 8 listopada 2016 w C i C++ przez alchem Obywatel (1,210 p.)

Cześć mam pewne zadanie:

Napisz wzorzec algorytmu Tokenizer, którego argumentami będzie podzakres kolekcji liter (kolekcja liter to może być obiekt typu std::string, ale też std::vector<char> itp.) oraz iterator docelowy, do jakiejś kolekcji napisów typu std::string. Algorytm ten powinien podzielić tekst z podzakresu kolekcji liter na wyrazy i umieścić je w kolekcji docelowej. Za definicję wyrazu przyjmujemy nieprzerwany ciąg liter lub cyfr. Uwaga: ten algorytm będzie miał dwa parametry we wzorcu, jeden na typ obu iteratorów opisujących zakres, drugi na typ iteratora docelowego. Można użyć jednej pętli (tylko jednej). 

 

Jednak nie za bardzo wiem jak mam wyglądać sama funkcja, zamiast na rozwiązaniu algorytmu chciałbym się skupić na tym jak taki algorytm powinien zostać zadeklarowany(nie wiem czy to dobre słowo).

Wymyśliłem takie coś :

template <class iterator>//szablon

void Tokenizer(iterator b , iterator e, iterator n )
{

}

jednak już przy pierwszy wywołaniu funkcji w teście kompilator uświadamia mi, że takiego argumentu użyć nie mogę. 

Tokenizer(std::begin(tekst),std::end(tekst),std::back_inserter(words));

Z góry dzięki za pomoc

3 odpowiedzi

+1 głos
odpowiedź 8 listopada 2016 przez adrian17 Ekspert (344,860 p.)
wybrane 8 listopada 2016 przez alchem
 
Najlepsza

Uwaga: ten algorytm będzie miał dwa parametry we wzorcu

No to dwa argumenty :)

template<class InputIterator, class OutputIterator>
void tokenize(InputIterator b , InputIterator e, OutputIterator n)

 

komentarz 8 listopada 2016 przez alchem Obywatel (1,210 p.)
(i tu wychodzi nieobecność na wykładzie)
Dziś już nie mam do tego głowy ale jutro powalczę  i jeśli byłbyś tak dobry, to się jeszcze tu odezwę, pewnie będę miał  jakieś pytanie odnośnie tego  :)
0 głosów
odpowiedź 12 listopada 2016 przez alchem Obywatel (1,210 p.)

Nadal usiłuję zrobić to zadanie, jednak, mam problem z przeniesieniem pojedynczych wyrazów do punktu docelowego:

Funkcja testująca wygląda tak:

void test_Tokenizer(void)
{
std::string tekst=
R"#(People are frustrated by PC kinks and the erratic behavior they
produce. Such unexpected variations in performance have long
been smoothed out in refrigerators, televisions, mobile phones,
and automobiles. As for PCs, telling users that their own
surfing or program installation choices are to blame
understandably makes them no less frustrated, even if they
realize that a more reliable system would inevitably be less
functional—a trade-off seemingly not required by refrigerator
improvements. Worse, the increasing reliance on the PC and
Internet that suggests momentum in their use means that more
is at risk when something goes wrong. Skype users who have
abandoned their old-fashioned telephone lines may regret their
decision if an emergency arises and they need to dial an
emergency number like 911, only to find that they cannot get
through, let alone be located automatically. When one’s
finances, contacts, and appointments are managed using a PC,
it is no longer merely frustrating if the computer comes down
with a virus. It is enough to search for alternative architectures.)#";

// Zittrain, The Future of the Internet, http://yupnet.org/zittrain/archives/14

std::list<std::string> words;
Tokenizer(std::begin(tekst),std::end(tekst),std::back_inserter(words));

std::list<std::string> wdone{"People","are","frustrated","by","PC","kinks",
"and","the","erratic","behavior","they","produce","Such","unexpected",
"variations","in","performance","have","long","been","smoothed","out",
"in","refrigerators","televisions","mobile","phones","and","automobiles",
"As","for","PCs","telling","users","that","their","own","surfing","or",
"program","installation","choices","are","to","blame","understandably",
"makes","them","no","less","frustrated","even","if","they","realize",
"that","a","more","reliable","system","would","inevitably","be","less",
"functional","a","trade","off","seemingly","not","required","by",
"refrigerator","improvements","Worse","the","increasing","reliance","on",
"the","PC","and","Internet","that","suggests","momentum","in","their",
"use","means","that","more","is","at","risk","when","something","goes",
"wrong","Skype","users","who","have","abandoned","their","old",
"fashioned","telephone","lines","may","regret","their","decision","if",
"an","emergency","arises","and","they","need","to","dial","an",
"emergency","number","like","911","only","to","find","that","they",
"cannot","get","through","let","alone","be","located","automatically",
"When","one","s","finances","contacts","and","appointments","are",
"managed","using","a","PC","it","is","no","longer","merely",
"frustrating","if","the","computer","comes","down","with","a","virus",
"It","is","enough","to","search","for","alternative","architectures"};

assert(words==wdone);

std::vector<char> tekst_jako_vector(std::begin(tekst),std::end(tekst));

std::set<std::string> words_list;
Tokenizer(std::begin(tekst_jako_vector),
          std::end(tekst_jako_vector),
          std::inserter(words_list,std::end(words_list))); // sprawdz w dokumentacji co to

std::list<std::string> wldone{"911","As","Internet","It","PC","PCs","People","Skype","Such",
"When","Worse","a","abandoned","alone","alternative","an","and","appointments",
"architectures","are","arises","at","automatically","automobiles","be","been","behavior",
"blame","by","cannot","choices","comes","computer","contacts","decision","dial","down",
"emergency","enough","erratic","even","fashioned","finances","find","for","frustrated",
"frustrating","functional","get","goes","have","if","improvements","in","increasing",
"inevitably","installation","is","it","kinks","less","let","like","lines","located","long",
"longer","makes","managed","may","means","merely","mobile","momentum","more","need","no",
"not","number","off","old","on","one","only","or","out","own","performance","phones",
"produce","program","realize","refrigerator","refrigerators","regret","reliable",
"reliance","required","risk","s","search","seemingly","smoothed","something","suggests",
"surfing","system","telephone","televisions","telling","that","the","their","them","they",
"through","to","trade","understandably","unexpected","use","users","using","variations",
"virus","when","who","with","would","wrong"};

assert(wldone.size()==words_list.size());
assert(std::equal(std::begin(words_list),std::end(words_list),std::begin(wldone)));
}

 

 

Mój kod, tak:

#include <iostream>
#include <algorithm>
#include <cassert>
#include <list>
#include <set>
#include <cctype>


using namespace std;

template <class InputIterator, class OutputIterator>//wejscie // wyjscie

void Tokenizer(InputIterator b, InputIterator e,  OutputIterator n )
{
    InputIterator help = b;
    b++;

    while(b != e)
    {
        if( (!(isdigit(*b))) && (!isalpha(*b))  )
        {
            copy(help,b, back_inserter(n));
            help = b;
        }
       b++;
    }
}


int main()
{
    test_Tokenizer();


}

Jednak przy poleceniu copy program się sypie, plus jest  kilka "warmingów"

Nie mam już pojęcia jak to zrobić, pomoże ktoś? 

1
komentarz 12 listopada 2016 przez adrian17 Ekspert (344,860 p.)

Przekombinowałeś.

do jakiejś kolekcji napisów typu std::string.

Tworzysz std::string i wsadzasz go do iteratora.

        if( (!(isdigit(*b))) && (!isalpha(*b))  )
        {
            *n = std::string(help, b);
            n++;
            help = b;
        }

(to wciąż nie tokenizuje idealnie, ale kluczowa część, czyli wsadzanie do iteratora, jest już poprawna.)

komentarz 12 listopada 2016 przez alchem Obywatel (1,210 p.)
Masz rację, teraz jest ok, jedynie muszę poprawić algorytm bo uwzględnia kropki i przecinki a tego nie chcę, jeszcze raz dzięki wielkie za pomoc.
0 głosów
odpowiedź 12 listopada 2016 przez alchem Obywatel (1,210 p.)

Gdyby kogoś ciekawiło, algorytm:

template <class InputIterator, class OutputIterator>//wejscie // wyjscie

void Tokenizer(InputIterator b, InputIterator e,  OutputIterator n )
{
    InputIterator help = find_if(b, e, Is_digit_or_alpha);
    InputIterator help2 = find_if(b, e, Is_not_digit_and_not_alpha);
    while(help != e)
    {

        *n = std::string(help, help2);
        n++;
        help = b+1;
        help = find_if(help2, e, Is_digit_or_alpha);
        help2 = find_if(help, e, Is_not_digit_and_not_alpha);
    }
}

 

Podobne pytania

–1 głos
1 odpowiedź 289 wizyt
0 głosów
2 odpowiedzi 191 wizyt
pytanie zadane 11 lutego 2016 w C i C++ przez k222 Nałogowiec (30,150 p.)
0 głosów
1 odpowiedź 247 wizyt
pytanie zadane 8 stycznia 2018 w C i C++ przez aabbcc Nowicjusz (150 p.)

92,555 zapytań

141,404 odpowiedzi

319,557 komentarzy

61,940 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...