PB schneller als C++

Fragen zu allen anderen Programmiersprachen.
Nik
Beiträge: 132
Registriert: 04.02.2005 19:57

Beitrag von Nik »

Das sollte in keinsterweise eine Anfang für einen Spam War werden, ich habe mich gefragt wie groß der Unterschied ist, habe dieses Programm geschrieben und mich gewundert also habe ich es gepostet was ist daran nun biotte verwerflich? Ich kann dir auch den Code schicken den ich in den letzten 3 Tagen geschrieben habe wenn du glaubst ich würde spammen oderso... Außerdem hoffte ich eben ein wenig auf Hilfe oder Kommentare was ich falsch gemacht habe, das dabei auch unkonstruktives gespamme bei raus kommt da kann ich doch nichts dafür....
www.KoMaNi.de
Eine kleine Gruppe von Hobby Programmierern, die gerade einen Instant Messenger natürlich in PureBasic schreiben.
MARTIN
Beiträge: 454
Registriert: 08.09.2004 14:03
Wohnort: Kiel

Beitrag von MARTIN »

@Spirit
Ich habe mich leider hier mächtig vertan.
Str() und Val() verwechselt, egal.
Vielleicht braucht jemand auch Val() in c++.

Ansonsten man kann , wenn man grade kein bock oder zeit hat eigene algorythmen zu entwickeln, auch sprintf(...) benutzen oder aus STL streamstring:

Code: Alles auswählen

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	stringstream steam;

	stream <<1458554;
	string a;
	a =  stream.str();
	cout << a << endl;
	return 0;
}
Vielleicht gibt es noch einen Weg.
Amilo 1667|Suse Linux 10.1_64bit/WinXP |PB 4.00/3.94
Leo
Beiträge: 420
Registriert: 26.10.2004 18:26

Beitrag von Leo »

Für alle möglichen Umwandlungen von Zahl zu String und String zu Zahl gibt es diese Funktion, die aber auch mit stringstreams arbeitet:

Code: Alles auswählen

template<typename T, typename S> inline T lexical_cast(const S& source)
{
	std::stringstream converter;
	T result;

	if(!(converter << source && converter >> result && (converter >> std::ws).eof()))
		throw std::logic_error("lexical_cast failed");

	return result;
}

Code: Alles auswählen

try
{
    std::string foo("1234");
    unsigned int bar = lexical_cast<unsigned int>(foo);
} 
catch(const std::exception& exp)
{
    std::cout << exp.what() << std::endl;
}

Nik
Beiträge: 132
Registriert: 04.02.2005 19:57

Beitrag von Nik »

thx
www.KoMaNi.de
Eine kleine Gruppe von Hobby Programmierern, die gerade einen Instant Messenger natürlich in PureBasic schreiben.
Benutzeravatar
Danilo
-= Anfänger =-
Beiträge: 2284
Registriert: 29.08.2004 03:07

Beitrag von Danilo »

Leo hat geschrieben:Für alle möglichen Umwandlungen von Zahl zu String und
String zu Zahl gibt es diese Funktion, die aber auch mit
stringstreams arbeitet:
Für BASIC-Coder, die Str() und Hex() gewohnt sind, hier
noch bissl was zum rumprobieren und rumspielen:

Code: Alles auswählen

/*
 *
 * Str(), Hex(), Oct(), Val()
 *
 */
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;



template<class T>
std::string Str(T x) {
   std::ostringstream stream;
   stream << x;
   return stream.str();
}

template<class T>
std::string Str(T x, int length, char fill=' ', bool left=false) {
   std::ostringstream stream;

   if(left) stream << setfill(fill) << setw(length) << std::left << x;
   else     stream << setfill(fill) << setw(length) << x;

   return stream.str();
}

template<class T>
std::string Hex(T x) {
   std::ostringstream stream;
   stream << std::hex << std::uppercase << x;
   return stream.str();
}

template<class T>
std::string Hex(T x, int length, char fill=' ', bool left=false) {
   std::ostringstream stream;

   if(left) stream << setfill(fill)  << setw(length) << std::hex
                   << std::uppercase << std::left    << x;
   else     stream << setfill(fill)  << setw(length) << std::hex
                   << std::uppercase << x;

   return stream.str();
}

template<class T>
std::string Oct(T x) {
   std::ostringstream stream;
   stream << std::oct << x;
   return stream.str();
}

template<class T>
std::string Oct(T x, int length, char fill=' ', bool left=false) {
   std::ostringstream stream;

   if(left) stream << setfill(fill) << setw(length) << std::oct << std::left << x;
   else     stream << setfill(fill) << setw(length) << std::oct << x;

   return stream.str();
}

template<class T>
T Val(std::string s) {
   T a;
   std::stringstream stream;
   stream << s;
   stream >> a;
   return a;
}

std::string Chr(unsigned char x) {
   std::ostringstream stream;
   stream << x;
   return stream.str();
}



int main(int argc, char *argv[])
{
  int                a = 12;
  float              b = 12.34f;
  unsigned char      c = 65;
  long               d = -1234567;
  unsigned long      e = 0xFFFFFFFF;
  long               f = 0xFFFFFFFF;
//  long long          g = 0x7FFFFFFFFFFFFFFF;
//  unsigned long long h = 0xFFFFFFFFFFFFFFFF;

  cout << "Dezimal: " << Str(a,10) << " Hex: " << Hex(a,8) << endl
       << "Dezimal: " << Str(b,10) << " Hex: " << Hex(b,8) << endl
       << "Dezimal: " << Str(c,10) << " Hex: " << Hex(c,8) << endl
       << "Dezimal: " << Str(d,10) << " Hex: " << Hex(d,8) << endl
       << "Dezimal: " << Str(e,10) << " Hex: " << Hex(e,8) << endl
       << "Dezimal: " << Str(f,10) << " Hex: " << Hex(f,8) << endl;
       //<< "Dezimal: " << Str(g,20) << " Hex: " << Hex(g,8) << endl
       //<< "Dezimal: " << Str(h,20) << " Hex: " << Hex(h,8) << endl;

  cout << endl;

  // string von datentyp erstellen
  string s = Str(a) + " -- " + Str(b);

  cout << "string: " << s << endl << endl;
  
  a=255;
  cout << Oct(a) << endl;
  
  int x = Val<int>("-123");
  cout << x << endl;

  float y = Val<float>("123.456");
  cout << y << endl;
  
  for(long i=0; i<256; i++) cout << Chr(i);

  system("PAUSE");	
  return 0;
}

Code: Alles auswählen

/*
 * ToString() und ToHex() Tests als Template Klasse
 *
 * Ausgabe:
 *
 *  Dezimal:                   10  Hex:                A
 *  Dezimal:                12.34  Hex:            12.34
 *  Dezimal:                    A  Hex:                A
 *  Dezimal:             -1234567  Hex:         FFED2979
 *  Dezimal:           4294967295  Hex:         FFFFFFFF
 *  Dezimal:                   -1  Hex:         FFFFFFFF
 *  Dezimal:  9223372036854775807  Hex: 7FFFFFFFFFFFFFFF
 *  Dezimal: 18446744073709551615  Hex: FFFFFFFFFFFFFFFF
 *  
 *  string: 10 -- 12.34
 *
 */
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdlib.h>
#include <string>

using namespace std;



template<class T>
class var {
  public:
    var()      { value=0; }
    var(T a)   { value=a; }
/*
    friend ostream &operator<<(ostream &stream, T &o) {
       return stream << o.value;
    }

    friend istream &operator>>(istream &stream, T &o) {
       return stream >> o.value;
    }
*/
    std::string ToString() {
       std::ostringstream stream;
       stream << this->value;
       return stream.str();
    }

    std::string ToString(int length, char fill=' ', bool left=false) {
       std::ostringstream stream;

       if(left) stream << setfill(fill) << setw(length) << std::left << value;
       else     stream << setfill(fill) << setw(length) << value;

       return(stream.str());
    }

    std::string ToHex() {
       std::ostringstream stream;
       stream << hex << uppercase << value;
       return stream.str();
    }

    std::string ToHex(int length, char fill=' ', bool left=false) {
       std::ostringstream stream;

       if(left) stream << setfill(fill)  << setw(length) << std::hex
                       << std::uppercase << std::left    << value;
       else     stream << setfill(fill)  << setw(length) << std::hex
                       << std::uppercase << value;

       return(stream.str());
    }

    operator std::string() {
       return this->ToString();
    }

    operator T() { return value; }
    
    T operator++()      { value++; return *this; }
    T operator++(int a) { value++; return *this; }
    T operator--()      { value--; return *this; }
    T operator--(int a) { value--; return *this; }

    T operator+=(int a) { value += a; return *this; }
    T operator-=(int a) { value -= a; return *this; }

  private:
    T value;
};



template<class T>
void Ausgabe(T x) {
  cout << "Dezimal: "
       << x.ToString(20)
       << "  Hex: "
       << x.ToHex(16)
       << endl;
}



int main(int argc, char *argv[])
{

  var<int>                a = 12;
  var<float>              b = 12.34f;
  var<char>               c = 65;
  var<long>               d = -1234567;
  var<unsigned long>      e = 0xFFFFFFFF;
  var<long>               f = 0xFFFFFFFF;
//  var<long long>          g = 0x7FFFFFFFFFFFFFFF;
//  var<unsigned long long> h = 0xFFFFFFFFFFFFFFFF;

  a++; // teste operatoren ++
  ++a; //                

  a--; // teste operatoren --
  --a; //                

  a += 1; // 12 + 1 = 13
  a -= 3; // 13 - 3 = 10


  Ausgabe(a);
  Ausgabe(b);
  Ausgabe(c);
  Ausgabe(d);
  Ausgabe(e);
  Ausgabe(f);
//  Ausgabe(g);
//  Ausgabe(h);

  cout << endl;

  // string von datentyp erstellen
  string s = a.ToString() + " -- " + b.ToString();

  cout << "string: " << s << endl << endl;

  system("PAUSE");	
  return 0;
}

Code: Alles auswählen

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class Integer {
  public:
    Integer()      { value=0; }
    Integer(int a) { value=a; }
    
    friend ostream &operator<<(ostream &stream, Integer &o) {
       return stream << o.value;
    }

    friend istream &operator>>(istream &stream, Integer &o) {
       return stream >> o.value;
    }

    operator string() {
       std::stringstream stream;
       stream << value;
       return stream.str();
    }
    
    operator int() { return value; }
    
    Integer operator++()      { value++; return *this; }
    Integer operator++(int a) { value++; return *this; }
    Integer operator--()      { value--; return *this; }
    Integer operator--(int a) { value--; return *this; }

    Integer operator+=(int a) { value += a; return *this; }
    Integer operator-=(int a) { value -= a; return *this; }

  private:
    int value;
};


int main(int argc, char *argv[])
{
  Integer a = 12;
  
  a++; // teste operatoren ++
  ++a; //                

  a--; // teste operatoren --
  --a; //                

  a += 1; // 12 + 1 = 13
  a -= 3; // 13 - 3 = 10
  
  cout << a << endl
       << "Bitte geben sie eine Integer-Zahl ein:  ";
  
  cin >> a;
  
  cout << a << endl;
  
  string x = a;
  
  cout << "Inhalt von String 'x':  " << x << endl;
  
  
  system("PAUSE");	
  return 0;
}
Wie gesagt, nur zum rumspielen für Umsteiger gedacht... ;)
cya,
...Danilo
"Ein Genie besteht zu 10% aus Inspiration und zu 90% aus Transpiration" - Max Planck
BrunoF
Beiträge: 23
Registriert: 05.09.2004 22:29

Beitrag von BrunoF »

hehe da sehe ich sofort warum ich als Gelegenheitsprogrammierer immer wieder auf BASIC zurückgekommen bin.
Benutzeravatar
Kiffi
Beiträge: 10711
Registriert: 08.09.2004 08:21
Wohnort: Amphibios 9

Beitrag von Kiffi »

> hehe da sehe ich sofort warum ich als Gelegenheitsprogrammierer immer
> wieder auf BASIC zurückgekommen bin.

LOL! Mir geht's genauso :-)

Grüße ... Kiffi
Antworten