C-plus-plus: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
Zeile 58: Zeile 58:
   const char* m_message;
   const char* m_message;
};
};
</syntaxhighlight>
= STL =
== String ==
Konstruktoren:
* string() https://cplusplus.com/reference/string/string/string/
* string(data)
* string(int count, char c)
Zugriff/Manipulation
* size_t length() Aktuelle Länge
* size_t capacity() Liefert aktuell Puffergröße
* void resize(size_t newLen, [char cc = '\0']) Setzt Länge durch Kürzen oder Auffüllen mit cc
* reserve(size_t newSize): Setzt Kapazität
* clear() Kürzt auf 0
* bool empty() Test auf Leerstring
* int compare(data) Vergleich: <0 0 >0 https://cplusplus.com/reference/string/string/compare/
* string substr(size_t pos, size_t len) Teilstring erstellen
* char& at(int position) Liefert Zeichen von Position
* char& back() Letztes Zeichen
* char& front() erstes Zeichen
* append(data) https://cplusplus.com/reference/string/string/append/
* push_back(char cc) Zeichen anfügen
* char pop_back() Letztes Zeichen entfernen und zurückgeben
* insert(size_t pos, data) https://cplusplus.com/reference/string/string/insert/
* string& erase(size_t pos, size_t len) Löschen https://cplusplus.com/reference/string/string/erase/
* iterator erase(iterator begin, iterator end) Löschen
Finden und Ersetzen
* replace(pos, len, data) https://cplusplus.com/reference/string/string/replace/
* size_t find(data) Substring finden https://cplusplus.com/reference/string/string/find/
* size_t rfind(data) Von hinten Substring finden
* find_first_of(char cc)
* find_last_of(char cc)
Konvertierung
* int stoi()  Wandlung in int
* const char* c_str() C-String
* string to_string(NUMERIC val) int, long, ... long double in String wandeln https://cplusplus.com/reference/string/to_string/
Iteratoren:
* begin
* end
* rbegin Vom Ende her
* rend
* cbegin Ein const-Iterator
* cend
* crbegin
* crend
<syntaxhighlight lang="c++">
#include <string>
std::string s;
for (auto rit=str.rbegin(); rit!=str.rend(); ++rit){
  std::cout << *rit;
}
for (auto it=str.begin(); it!=str.end(); ++it)[
    std::cout << *it;
}
for ( auto it=str.cbegin(); it!=str.cend(); ++it){
    std::cout << *it;
}
</syntaxhighlight>
== Vector ==
Konstruktoren: https://cplusplus.com/reference/vector/vector/vector/
* vector()
* vector(size_type n [data]) Größe festlegen, mit data füllen
* vector(data) Liste
<syntaxhighlight lang="c++">
</syntaxhighlight>
== RegExpr ==
<syntaxhighlight lang="c++">
</syntaxhighlight>
</syntaxhighlight>

Version vom 8. Dezember 2022, 18:23 Uhr


Links

Klassen

Beispiel Klasse Complex

class Complex
{
public:
    double _real;
    double _imag;
public:
    Complex(): _real(0), _imag(0) {}
    Complex(double real, double imag):  _real(real), _imag(imag) {}
    Complex(const Complex &src):  _real(src._real), _imag(src._imag) {}
    Complex& operator =(const Complex& source){
        _real = source._real;
        _imag = source._imag;
        return *this;
    }
};

Beispiel Interface

class Box {
   public:
      // pure virtual function
      virtual double getVolume() = 0;
};

Exception

class ReException {
public:
  ReException(const char* message);
  ReException(const char* message, const char* file, int line);
  virtual ~ReException();
public:
  ReException(const ReException& source): _message(source._message){};
  ReException& operator =(const ReException& source){ _message = source._message; }
protected:
  ReException();
public:
  inline void setMessage(const char* message) {
    if (m_message != NULL)
      free((void *) m_message);
      m_message = _strdup(message);
    }
  }
  inline const char* getMessage() const {
    return m_message;
  }
protected:
  const char* m_message;
};