![]() |
![]() |
|
Programiranje Programski jezici, tehnike, alatke... |
![]() |
|
Alatke vezane za temu | Vrste prikaza |
![]() |
#121 |
V.I.P. Programiranje
Član od: 29.8.2007.
Lokacija: Valjevo
Poruke: 1.349
Zahvalnice: 983
Zahvaljeno 371 puta na 280 poruka
|
![]()
@Ivan
Ok. Mada mislim da je kod ocigledan. Kod:
class random ... Klasa ima templejt metodu get overload-ovanu da bih u zavisnosti od tipa argumenata koristio odgovarajuce "distribucije". Znaci samo integral i floating-point tipovi dolaze u obzir sve ostalo je compile time error. http://en.cppreference.com/w/cpp/num...t_distribution Kod:
typedef std::pair<std::size_t, std::size_t> tpnum; std::vector<tpnum> numbers; Kod:
numbers.resize(N); std::for_each(std::begin(numbers), std::end(numbers), [](tpnum& e){ e = std::make_pair(0u, random::get(0u, 100u)); }); Kod:
for(auto& it : numbers) { it.first = std::count_if(std::begin(numbers), std::end(numbers), [&it](const tpnum& e){ return it.second == e.second; }); } Kod:
std::sort(std::begin(numbers), std::end(numbers), [](const tpnum& l, const tpnum& r){ return l.second > r.second; }); Kod:
auto end = std::unique(std::begin(numbers), std::end(numbers), [](const tpnum& l, const tpnum& r){ return l.second == r.second; }); Kod:
std::sort(std::begin(numbers), end, [](const tpnum& l, const tpnum& r){ return l.first > r.first; }); I na kraju ispisujem prvih F elemenata. Mada moze da se desi da broj unikata bude manji od F-a ali nece fejlovati. |
![]() |
![]() |
![]() |
#122 |
Veteran
Član od: 3.5.2008.
Lokacija: Beograd
Poruke: 760
Zahvalnice: 81
Zahvaljeno 213 puta na 144 poruka
|
![]()
Pa dobro, jeste tačno, mada malo opskurno. Ali šta ako se traži veliki broj ponavljanja, koliki je i potreban da bi ispitivanje imalo statističku vrednost. Recimo nekoliko desetina hiljada. Uporedi performanse. Rakun_s_Neonkom je dao najbolji način za vođenje broja ponavljanja iz koga prirodno proizilazi samo jedno jedino sortiranje i kraj.
![]() Poslednja ispravka: ivan90BG (24.9.2012 u 9:34) |
![]() |
![]() |
Sledećih 2 korisnika se zahvaljuje korisniku ivan90BG na korisnoj poruci: | ||
Belphegor (24.9.2012), RaKuN_s_NeOnKoM (27.9.2012) |
![]() |
#123 |
Veteran
Član od: 3.5.2008.
Lokacija: Beograd
Poruke: 760
Zahvalnice: 81
Zahvaljeno 213 puta na 144 poruka
|
![]()
Ima li neko predstavu zašto bi linker bacao grešku da ne može da nađe destruktor nadklase (pozvan je iz destruktora podklase), ako je pri tom taj destruktor označen sa "=0" (cela klasa je interfejs). Kompajler ne bi trebalo ni da ubacuje poziv za destruktor nad klase ako je pure virtual.
|
![]() |
![]() |
![]() |
#125 |
Veteran
Član od: 3.5.2008.
Lokacija: Beograd
Poruke: 760
Zahvalnice: 81
Zahvaljeno 213 puta na 144 poruka
|
![]()
OK, razumeo, čak i ako je pure virtual destruktor mora da ima implementaciju. (
![]() |
![]() |
![]() |
![]() |
#126 |
V.I.P. GNU/Linux
Član od: 1.11.2005.
Poruke: 11.131
Zahvalnice: 2.058
Zahvaljeno 4.911 puta na 2.848 poruka
|
![]()
To si ti i postavio, samo da proveriš naš googling skillz
![]() |
![]() |
![]() |
![]() |
#127 |
V.I.P. GNU/Linux
Član od: 1.11.2005.
Poruke: 11.131
Zahvalnice: 2.058
Zahvaljeno 4.911 puta na 2.848 poruka
|
![]()
Neke fore i fazoni za optimizaciju C i C++ koda (nije baš najsvežije, ali ima dosta toga korisnog):
http://www.tantalon.com/pete/cppopt/main.htm |
![]() |
![]() |
Sledeći korisnik se zahvaljuje korisniku voodoo_ na korisnoj poruci: | ||
Nikola Stankovic (21.10.2012) |
![]() |
#128 |
Kekule Mekule
|
![]()
Imam problem sa templejtima posto ne mogu nigde da nadjem krsten primer za ono sto meni treba. Elem imam Vec2D klasu koja je templejt i kod je sledeci:
Kod:
#ifndef VEC2D_HPP_INCLUDED #define VEC2D_HPP_INCLUDED #include <cmath> template <typename T> class Vec2d { public: // Constructor Vec2d(); Vec2d(T x, T y); Vec2d(const Vec2d& v); // Methods T GetLength(); Vec2d GetNormalized(); // Operators Vec2d& operator=(const Vec2d& v); Vec2d& operator+=(const Vec2d& v); Vec2d& operator-=(const Vec2d& v); Vec2d& operator*=(const T s); Vec2d& operator/=(const T s); const Vec2d operator+(const Vec2d& v) const; const Vec2d operator-(const Vec2d& v) const; const Vec2d operator*(const T s)const; const Vec2d operator/(const T s)const; bool operator==(const Vec2d& v); bool operator!=(const Vec2d& v); // Member variables T X; T Y; // Static members static Vec2d Zero; }; #endif // VEC2D_HPP_INCLUDED Kod:
#include "Vec2d.hpp" // Static member initialization template <typename T> Vec2d<T>::Zero = Vec2d<T>(); // Constructor template <typename T> Vec2d<T>::Vec2d() { X = 0; Y = 0; } template <typename T> Vec2d<T>::Vec2d(T x, T y) { X = x; Y = y; } template <typename T> Vec2d<T>::Vec2d(const Vec2d& v) { X = v.X; Y = v.Y; } // Method implementation template <typename T> T Vec2d<T>::GetLength() { return sqrt(X * X + Y * Y); } template <typename T> Vec2d<T> Vec2d<T>::GetNormalized() { float len = GetLength(); return Vec2d<T>(X / len, Y / len); } // Operator implementation template <typename T> Vec2d<T>& Vec2d<T>::operator=(const Vec2d& v) { if(this != &v) { X = v.X; Y = v.Y; } return *this; } template <typename T> Vec2d<T>& Vec2d<T>::operator+=(const Vec2d& v) { X += v.X; Y += v.Y; return *this; } template <typename T> Vec2d<T>& Vec2d<T>::operator-=(const Vec2d& v) { X -= v.X; Y -= v.Y; return *this; } template <typename T> Vec2d<T>& Vec2d<T>::operator*=(const T s) { X *= s; Y *= s; return *this; } template <typename T> Vec2d<T>& Vec2d<T>::operator/=(const T s) { X /= s; Y /= s; return *this; } template <typename T> const Vec2d<T> Vec2d<T>::operator+(const Vec2d& v) const { return Vec2d<T>(*this) += v; } template <typename T> const Vec2d<T> Vec2d<T>::operator-(const Vec2d& v) const { return Vec2d<T>(*this) -= v; } template <typename T> const Vec2d<T> Vec2d<T>::operator*(const T s) const { return Vec2d<T>(*this) *= s; } template <typename T> const Vec2d<T> Vec2d<T>::operator/(const T s) const { return Vec2d<T>(*this) /= s; } template <typename T> bool Vec2d<T>::operator==(const Vec2d& v) { if(X == v.X && Y == v.Y) return true; return false; } template <typename T> bool Vec2d<T>::operator!=(const Vec2d& v) { return !(*this == v); } Ukoliko uklonim staticku promenljivu, kompajl prodje fino i prilikom linkovanja popijem gomilu undefined reference gresaka. Moja pitanja: 1. Gde sve pisem <T>? Kod povratne vrednosti i imena klase ciji metod implementiram i nigde vise? 2. Kako inicijalizujem staticku promenljivu templejta? Jel potrebno da pravim specijalizaciju za int, float i ostale tipove koje budem nameravao da koristim (videh negde da se to radi)? 3. Zbog cega se pojavljuju undefined reference greske i kako ih ukloniti? |
![]() |
![]() |
![]() |
#129 |
V.I.P. Programiranje
Član od: 29.8.2007.
Lokacija: Valjevo
Poruke: 1.349
Zahvalnice: 983
Zahvaljeno 371 puta na 280 poruka
|
![]()
Dok ne budes dobio detaljniji odgovor od nekog da probam ja da pomognem.
2. Sintaksa je: Kod:
template < typename T > typename Vec2d<T>::Vec2d Vec2d<T>::Zero = Vec2d<T>(); Evo ti recimo za operatore "uporedjivanja" (g++ 4.7), ovi operatori trebaju isto da budu const posto ne menjaju clanove klase. Kod:
#include <type_traits> ... template < typename T > class Vec2d { // primamo samo odredjene tipove templejta Vec2d // ostalo je compile time error static_assert( std::is_floating_point<T>::value || std::is_integral<T>::value, "Unacceptable type for Vec2d!"); public: Vec2d() : x(0), y(0) {} Vec2d(T _x, T _y) : x(_x), y(_y) {} template < typename U > typename std::enable_if< std::is_floating_point<T>::value || std::is_floating_point<U>::value, bool >::type operator == (const Vec2d<U>& o) const { if((std::fabs(x - o.x) > 0.00001) || (std::fabs(y - o.y) > 0.00001)) { return false; } return true; } template < typename U > typename std::enable_if< std::is_integral<T>::value && std::is_integral<U>::value, bool >::type operator == (const Vec2d<U>& o) const { return x == o.x && y == o.y; } bool operator != (const Vec2d<T>& o) const { return !(*this == o); } T x, y; static Vec2d<T> Zero; }; 3. Daj celu poruku za sta kuka. |
![]() |
![]() |
Sledeći korisnik se zahvaljuje korisniku Belphegor na korisnoj poruci: | ||
Andross (29.10.2012) |
![]() |
#130 |
Kekule Mekule
|
![]()
Hvala za ovo.
Linker errore sam resio instanciranjem templejt klasa posto mi je implementacija i dalje odvojena, a koristicu samo odredjene tipove za T: Kod:
template class Vec2d<int>; template class Vec2d<float>; 1. Zasto si koristio typename U za operator == 2. Koja je fora sa const pre i posle funkcije? Tipa const a(); b() const; const c() const; Poslednja ispravka: Andross (29.10.2012 u 22:15) |
![]() |
![]() |
![]() |
#131 | |
V.I.P. Programiranje
|
![]() Citat:
2. Const pre funkcije označava da je povratna vrednost funkcije konstantna tj. da se ne može menjati. Const posle funkcije znači da funkcija ne može da menja stanje klase u kojoj se nalazi, pa može da se poziva i iz const objekata te klase. Na primer: Kod:
class cls { public: int member1, member2; void do_something() { member1 = 40; member2 = 20; } int do_something_const() const { return member1*member2; } /* ne menja stanje klase */ }; ... const cls a; cls b; int x = a.do_something_const(); /* može, jer je funkcija const */ b.do_something(); /* može, funkcija nije const ali nije ni objekat */ a.do_something(); /* greška, objekat je const a funkcija nije! */ ... |
|
![]() |
![]() |
Sledeći korisnik se zahvaljuje korisniku Geomaster na korisnoj poruci: | ||
Andross (29.10.2012) |
![]() |
#132 |
V.I.P. Programiranje
Član od: 29.8.2007.
Lokacija: Valjevo
Poruke: 1.349
Zahvalnice: 983
Zahvaljeno 371 puta na 280 poruka
|
![]()
1. Zato sto nisam mogao drugacije da overloadujem taj operator na kontu samo T-a, a usput side-effect je da sad mogu da se porede Vec2d razlicith tipova:
Kod:
typedef Vec2d<double> vd; typedef Vec2d<int> vi; ... vd v0(0.2, 3.6); vi v1(5, 1); if(v0 == v1) ... Kod:
class Foo { mutable int data; float data2; ... float metod() const { data = 5;// sad je dozvoljeno return data2; } }; Poslednja ispravka: Belphegor (29.10.2012 u 23:00) |
![]() |
![]() |
Sledeći korisnik se zahvaljuje korisniku Belphegor na korisnoj poruci: | ||
Andross (29.10.2012) |
![]() |
#133 |
Veteran
Član od: 21.6.2006.
Poruke: 618
Zahvalnice: 629
Zahvaljeno 33 puta na 26 poruka
|
![]()
Pozdrav narode,
I nakon dugog pretrazivanja, nisam uspeo da nadjem ono sto me zanima: da li postoji nacin (i koji je to nacin) da ispisem tekst na ekran u boji po zelji, na osnovu R,G,B vrednosti? (Uspevao sam da nadjem razne enume i slicne nacine, ali to mi nije prakticno) Hvala u svakom slucaju ! ![]() |
![]() |
![]() |
![]() |
#134 |
V.I.P. Programiranje
Član od: 29.8.2007.
Lokacija: Valjevo
Poruke: 1.349
Zahvalnice: 983
Zahvaljeno 371 puta na 280 poruka
|
![]()
Evo ti za pocetak:
Kod:
#define WIN32_LEAN_AND_MEAN #include <Windows.h> #include <string> typedef std::basic_string<TCHAR> tstring; const tstring helloStr = TEXT("hello"); const tstring worldStr = TEXT("world"); LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); SetBkMode(hdc, TRANSPARENT); SetTextColor(hdc, RGB(200,0,100)); TextOut(hdc, 10, 10, helloStr.c_str(), helloStr.size()); SetTextColor(hdc, RGB(0,100,50)); TextOut(hdc, 10, 30, worldStr.c_str(), worldStr.size()); EndPaint(hWnd, &ps); return 0; }break; case WM_DESTROY: { PostQuitMessage(0); return 0; }break; }; return DefWindowProcW(hWnd, msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wndClass; memset(&wndClass, 0, sizeof(WNDCLASS)); wndClass.hInstance = GetModuleHandle(0); wndClass.style = CS_HREDRAW | CS_VREDRAW; wndClass.lpfnWndProc = WindowProc; wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); wndClass.lpszClassName = TEXT("TextTestClass"); wndClass.hbrBackground = GetSysColorBrush(COLOR_3DFACE); DWORD style = WS_SYSMENU | WS_BORDER | WS_CAPTION | WS_CLIPSIBLINGS | WS_MINIMIZEBOX | WS_THICKFRAME; style |= WS_VISIBLE; if(0 == RegisterClass(&wndClass)) { MessageBox(0, TEXT("RegisterClass() failed!"), 0, 0); return 1; } RECT clientSize; clientSize.top = 0; clientSize.left = 0; clientSize.right = 200; clientSize.bottom = 100; AdjustWindowRect(&clientSize, style, FALSE); int realWidth = clientSize.right - clientSize.left; int realHeight = clientSize.bottom - clientSize.top; int windowLeft = (GetSystemMetrics(SM_CXSCREEN) - realWidth) / 2; int windowTop = (GetSystemMetrics(SM_CYSCREEN) - realHeight) / 2; HWND hwnd = CreateWindowEx(0, TEXT("TextTestClass"), TEXT("Text test"), style, windowLeft, windowTop, realWidth, realHeight, NULL, NULL, GetModuleHandle(0), NULL); if(nullptr == hwnd) { MessageBox(0, TEXT("CreateWindowEx() failed!"), 0, 0); return 1; } ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd); MSG msg = {0}; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } |
![]() |
![]() |
![]() |
#135 |
Veteran
Član od: 3.5.2008.
Lokacija: Beograd
Poruke: 760
Zahvalnice: 81
Zahvaljeno 213 puta na 144 poruka
|
![]()
A sada nešto neviđeno
Kod:
class Foo { protected: struct Data { int var; }; Foo::Data* d; private: Foo(Foo::Data* dptr); public: static Foo New(); Foo(Foo& other); }; Foo::Foo(Foo::Data *dptr): d(dptr) {} Foo::Foo(Foo& other): d(other.d) {} Foo Foo::New() { return Foo(new Foo::Data); } int main() { Foo foo = Foo::New(); return 0; } main.cpp:26: error: no matching function for call to 'Foo::Foo(Foo)' linija 26 je u funkciji New(), a istu grešku javlja i za main() funkciju. Šta mu dođe ovo, copy constructor by value (što nema smisla). Ako stvarno dodam konstruktor Foo(Foo other) kompajler se buni. Izgleda kao da mu nešto fali, pa on prelazi na neki praistorijski default koga nema. Ima li neko ideju šta ovde nedostaje. Da nije možda stvar u tome što pri vraćanju objekata mora da se pozove copy constructor koji uzima referencu, a reference se mogu napraviti samo na neprivremene objekte. Mada čisto sumnjam. EDIT: Otkrio, copy constructor mora da uzima const& da bi mogao da radi sa privremenim objektima, šta ti je ovaj C++ ![]() Poslednja ispravka: ivan90BG (8.11.2012 u 1:29) |
![]() |
![]() |
![]() |
#136 |
Član
Član od: 21.7.2012.
Lokacija: Srbija
Poruke: 31
Zahvalnice: 5
Zahvaljeno jedanput na jednoj poruci
|
![]()
Ljudi, treba mi pomoc oko stringova.
Kako da uporedim string zapamcen u nizu od 15 karaktera sa nekim konstantnim stringom? Skontao sam da mogu da uporedjujem skaki pojedinacni karakter (if (ff[0]=='T' && itd... do '\0')) Ali to je naporno jer ima mnogo konstantnih stringova sa kojim treba uporediti. Moze tako da radi jedino ako napravim funkciju u koja ce da uporedjuje 2 stringa koja jos se posalju, ali sigurno postoji jednostavniji nacin, zar ne? |
![]() |
![]() |
![]() |
#137 |
Starosedelac
Član od: 23.2.2006.
Lokacija: Tamo gde su kuće od čokolade i prozori od marmelade....
Poruke: 1.579
Zahvalnice: 195
Zahvaljeno 301 puta na 171 poruka
|
![]() |
![]() |
![]() |
![]() |
#138 |
V.I.P. GNU/Linux
Član od: 1.11.2005.
Poruke: 11.131
Zahvalnice: 2.058
Zahvaljeno 4.911 puta na 2.848 poruka
|
![]()
Radi sigurnosti, koristi strncmp (kao treći argument prosledi dužinu prvog stringa od 15 karaktera).
|
![]() |
![]() |
![]() |
#139 |
Član
Član od: 10.9.2008.
Lokacija: Beograd
Poruke: 81
Zahvalnice: 0
Zahvaljeno 0 puta na 0 poruka
|
![]()
Jel bi mogao neko da mi za ovaj algoritam tj kod da mi napise ulaz i izlaz tj da program moze da cita sa standardnog ulaza i izbacuje resenje na standardni izraz. Ja bi to sam uradio al neki delovi mi nisu bas jasni pa ce mi ovako biti lakse da posmatram posle u kompajleru korak po korak sta program radi pa da razumem. Hvala unapred
![]() Kod:
#include <string> #include <cctype> using namespace std; class Parser { string input; bool isValid; bool isEnd(size_t pos) const { return pos >= input.size(); } bool readChar(size_t &pos, char c) const { if (isEnd(pos) || input[pos] != c) { return false; } ++pos; return true; } bool readString(size_t &pos, const string &str) const { if (input.substr(pos, str.size()) != str) { return false; } pos += str.size(); return true; } bool isLetter(size_t pos) const { return isEnd(pos) == false && isalpha(input[pos]); } bool isAlphanumeric(size_t pos) const { return isEnd(pos) == false && isalnum(input[pos]); } bool readPropositionalLetter(size_t &pos) const { if (isLetter(pos) == false) { return false; } do { ++pos; } while (isLetter(pos)); return true; } bool readSubformula(size_t &pos) const { if (isEnd(pos)) { return false; } if (readChar(pos, '-')) { return readSubformula(pos); } if (readChar(pos, '(')) { return readSubformula(pos) && readChar(pos, '>') && readSubformula(pos) && readChar(pos, ')'); } return readPropositionalLetter(pos); } bool findImplication(size_t &pos) const { int brackets = 0; for (; isEnd(pos) == false; ++pos) { if (brackets == 0 && input[pos] == '>') { ++pos; return true; } if (input[pos] == '(') { ++brackets; } else if (input[pos] == ')') { --brackets; } } return false; } public: Parser(const string &input) : input(input) { size_t pos = 0; if (readSubformula(pos) == false) { isValid = false; return; } if (isEnd(pos)) { isValid = true; return; } pos = 0; isValid = readSubformula(pos) && readChar(pos, '>') && readSubformula(pos) && isEnd(pos); } bool axiom1() const { if (isValid == false) { return false; } if (input.size() >= 2 && input[0] == '(' && input[input.size() - 1] == ')') { Parser tmp(input.substr(1, input.size() - 2)); if (tmp.axiom1()) { return true; } } size_t pos = 0; if (findImplication(pos) == false) { return false; } string a = input.substr(0, pos - 1); return readChar(pos, '(') && findImplication(pos) && readString(pos, a + ")") && isEnd(pos); } bool axiom2() const { if (isValid == false) { return false; } if (input.size() >= 2 && input[0] == '(' && input[input.size() - 1] == ')') { Parser tmp(input.substr(1, input.size() - 2)); if (tmp.axiom2()) { return true; } } size_t pos = 0; if (readChar(pos, '(') == false || findImplication(pos) == false) { return false; } string a = input.substr(1, pos - 2); if (readChar(pos, '(') == false || findImplication(pos) == false) { return false; } string b = input.substr(a.size() + 3, pos - a.size() - 4); pos = 0; if (findImplication(pos) == false) { return false; } string c = input.substr(a.size() + b.size() + 4, pos - a.size() - b.size() - 7); return readString(pos, string("((") + a + ">" + b + ")>(" + a + ">" + c + "))") && isEnd(pos); } bool axiom3() const { if (isValid == false) { return false; } size_t pos = 0; if (readString(pos, "((")) { Parser tmp(input.substr(1, input.size() - 2)); return tmp.axiom3(); } if (readString(pos, "(-") == false || findImplication(pos) == false) { return false; } string b = input.substr(2, pos - 3); pos = 0; if (findImplication(pos) == false) { return false; } string a = input.substr(b.size() + 4, pos - b.size() - 6); return readString(pos, string("(") + a + ">" + b + ")") && isEnd(pos); } }; |
![]() |
![]() |
![]() |
#140 |
Veteran
Član od: 27.12.2005.
Lokacija: Vremenske Grobnice, Hiperion
Poruke: 680
Zahvalnice: 99
Zahvaljeno 124 puta na 82 poruka
|
![]()
Ovako nešto:
Kod:
#include <iostream> #include <iomanip> #include <iterator> // ... parser kod ... // int main() { std::string input(std::istream_iterator<std::string::value_type>(std::cin), std::istream_iterator<std::string::value_type>()); Parser parser(input); std::cout << std::boolalpha; std::cout << parser.axiom1() << "\n"; std::cout << parser.axiom2() << "\n"; std::cout << parser.axiom3() << "\n"; return 0; } |
![]() |
![]() |
![]() |
Bookmarks sajtovi |
Tagovi |
c++, how to, pomoc, programiranje |
Alatke vezane za temu | |
Vrste prikaza | |
|
|