Está en la página 1de 6

Gua de Referencia Rapida de C++ (std)

Programa C++ Entrada / Salida Basica


// Inclusion de bibliotecas #include <iostream>
// Utilizacion de espacios de nombre #include <string>
// Definicion de Constantes y Tipos using namespace std;
// Definicion de subprogramas int main()
int main() {
{ int x, y;
// cuerpo del programa principal cin >> x >> y;
} cout << "Resultado: " << x << " " << y << endl;
cin.ignore(1000, '\n');
// g++ -ansi -Wall -Wextra -Werror -o main main.cpp
cin >> ws;
char a;
cin.get(a);
Tipos Simples string nombre;
getline(cin, nombre);
bool char short int unsigned float double enum }
Color { ROJO, VERDE, AZUL };

Constantes, Variables, Asignacion Estructuras de Control


if ( expr_logica ) {
const double PI = 3.1415;
...
int main()
} else if ( expr_logica ) {
{
...
char a, b; // sin inicializar
} else {
Color c; // sin inicializar
...
int x = 1; // inicializacion x = x * 2; // }
asignacion
switch ( expr_ordinal ) {
a = 'z'; // asignacion
case VALOR:
c = ROJO; // asignacion
...
x = int(PI); // asig, conversion
break;
}
default:
...
Operadores: Precedencia, Asociatividad
break;
Operador Tipo de Operador Asociatividad }
[] -> . Binarios Izq. a Dch. for ( unsigned i = 0; i < NELMS; ++i ) {
!-* Unarios Dch. a Izq. ...
*/% Binarios Izq. a Dch. }
+- Binarios Izq. a Dch.
while ( expr_logica ) {
< <= > >= Binarios Izq. a Dch.
== != Binarios Izq. a Dch. ...
&& Binario Izq. a Dch. }
|| Binario Izq. a Dch. do {
?: Ternario Dch. a Izq. ...
} while ( expr_logica );
Asignacion e Incrementos
Subprogramas
Sentencia Equivalencia
++vble; vble = vble + 1; int funcion(int p_valor_1, const Fecha& p_ref_cte_2) {
--vble; vble = vble - 1;
vble++; vble = vble + 1;
...
vble--; vble = vble - 1;
vble += expr; vble = vble + (expr);
return valor;
vble -= expr; vble = vble - (expr); }
vble *= expr; vble = vble * (expr); void procedimiento(int& p_ref_1, Fecha& p_ref_2)
vble /= expr; vble = vble / (expr); {
vble %= expr; vble = vble % (expr); ...
}
int main()
{
Fecha f = { 29, 6, 2011 };
int x = funcion(3, f);
Secciones opcionales marcadas con (*) procedimiento(x, f);
zPor cuestiones de espacio, en algunas ocasiones el codigo }
fuente no sigue un estilo de codi cacion adecuado.

g++ -ansi -Wall -Wextra -Werror -g -D GLIBCXX DEBUG -o programa programa.cpp 2>&1|less
Tipo String Tipo Array 2 Dimensiones (std)
#include <iostream> #include <array>
#include <string> using namespace std;
using namespace std; const unsigned NFILS = 2;
int main() const unsigned NCOLS = 3;
{ typedef array<int, NCOLS> Fila;
string nm = "pepe luis"; typedef array<Fila, NFILS> Matriz;
if (nm <= "lucas") { int main()
nm += " lopez"; {
} Matriz m = {{
nm = nm.substr(5, 4) + "miguel"; {{ 1, 2, 3 }},
for (int i = 0; i < nm.size(); ++i) { {{ 4, 5, 6 }}
nm[i] = char(nm[i] + 1); }};
} Matriz aux = m;
cin >> nm; // getline(cin , nm); cout if (m <= aux) {
<< nm << endl; aux = m;
} }
for (int f = 0; f < m.size(); ++f) {
Tipo Registro
for (int c = 0; c < m[f].size(); ++c) { m[f][c] =
struct Fecha { m[f][c] * 2;
unsigned dia; }
unsigned mes; }
unsigned anyo; }
};
int main() Ficheros: Salida de Datos
{ #include <string>
Fecha f = { 29, 6, 2011 }; #include <fstream>
Fecha hoy = f; using namespace std;
hoy = f; int main()
hoy.dia = f.dia; {
hoy.mes = f.mes; string nombre = "datos.txt";
hoy.anyo = f.anyo; ofstream f_out;
} f_out.open(nombre.c_str());
Tipo Array (std) if ( ! f_out.fail() ) {
f_out << "...datos..." << endl;
#include <array>
bool ok = ! f_out.fail();
using namespace std;
f_out.close();
const unsigned NELMS = 20;
typedef array<int, NELMS> Dat; }
int main() }
{
Dat d = {{ 1, 2, 3 }}; Ficheros: Entrada de Datos
Dat aux = d;
#include <string>
if (d <= aux) {
#include <fstream>
aux = d;
using namespace std;
}
int main()
for (int i = 0; i < d.size(); ++i) { {
d[i] = d[i] * 2;
string nombre = "datos.txt";
}
ifstream f_in;
}
f_in.open(nombre.c_str());
Salida de Datos Formateada(*) if ( ! f_in.fail() ) {
#include <iostream> string datos;
#include <iomanip> f_in >> datos;
using namespace std; bool ok = ! f_in.fail() || f_in.eof(); f_in.close();
int main()
{ }
cout << dec << 27 ; }
cout << hex << 27 ; Asertos(*)
cout << oct << 27 ;
cout << setprecision(2) << 4.567 ; cout #include <cassert>
<< setw(5) << 234 ; int main()
cout << setfill('#') << setw(5) << 234 ; {
} assert((x > 3)&&(y < 6));
}

g++ -ansi -Wall -Wextra -Werror -g -D GLIBCXX DEBUG -o programa programa.cpp 2>&1|less
Modulos TAD: Tipo Abstracto de Datos
//--- modulo.hpp ----------------------- //--- modulo.hpp -----------------------
#ifndef modulo_hpp_ #ifndef modulo_hpp_
#define modulo_hpp_ #define modulo_hpp_
#include <string> #include <string>
namespace umalcc { namespace umalcc {
struct Dato { class Dato {
std::string xx; public:
}; ~Dato() ;//destructor
void put(Dato& d, const std::string& x); Dato() ;//constructor
std::string get(const Dato& d); Dato(const Dato& o) ; //constructor copia
} Dato(const std::string& x) ;//no uso
#endif Dato& operator=(const Dato& o) ;//oper asig
//--- modulo.cpp ----------------------- void put(const std::string& x) ;
#include "modulo.hpp" std::string get() const ;
using namespace std; private:
namespace umalcc { std::string xx;
void put(Dato& d, const string& x) };
{ }
d.xx = x; #endif
} //--- modulo.cpp -----------------------
string get(const Dato& d) #include "modulo.hpp"
{ using namespace std;
return d.xx; namespace umalcc {
} Dato::~Dato() {}
} Dato::Dato() : xx() {}
//--- main.cpp ------------------------- Dato::Dato(const Dato& o) : xx(o.xx) {}
#include <iostream> Dato::Dato(const string& x) : xx(x) {}
#include "modulo.hpp" Dato& Dato::operator=(const Dato& o)
using namespace std; {
using namespace umalcc; if (this != &o) {
int main() xx = o.xx;
{ }
Dato d; return *this;
put(d, "pepe"); }
cout << get(d) << endl; void Dato::put(const string& x)
} {
// g++ -ansi -Wall -Wextra -Werror -c modulo.cpp xx = x;
// g++ -ansi -Wall -Wextra -Werror -c main.cpp }
// g++ -o main main.o modulo.o string Dato::get() const
{
Memoria Dinamica return xx;
}
struct Nodo; }
typedef Nodo* PNodo; //--- main.cpp -------------------------
struct Nodo { #include <iostream>
PNodo sig; #include "modulo.hpp"
int dato; using namespace std;
}; using namespace umalcc;
int main() int main()
{ {
PNodo ptr = new Nodo; Dato d;
PNodo ptr2 = ptr; d.put("pepe");
ptr->sig = NULL; cout << d.get() << endl;
ptr->dato = 3; }
Nodo n = *ptr; // g++ -ansi -Wall -Wextra -Werror -c modulo.cpp
if ((ptr != NULL)&&(ptr == ptr2)) { // g++ -ansi -Wall -Wextra -Werror -c main.cpp
ptr = ptr->sig; // g++ -o main main.o modulo.o
}
delete ptr;
}

g++ -ansi -Wall -Wextra -Werror -g -D GLIBCXX DEBUG -o main main.cpp modulos.cpp ... 2>&1|less
POO: Clase Base POO: Clase Derivada
//--- base.hpp ----------------------- //--- derivada.hpp -----------------------
#ifndef base_hpp_ #ifndef derivada_hpp_
#define base_hpp_ #define derivada_hpp_
#include <string> #include <string>
namespace umalcc { namespace umalcc {
class Base { class Derivada : public Base {
public: public:
Base() ; Derivada() ;
Base(const std::string& x) ; Derivada(const std::string& x, int z) ; virtual
virtual ~Base() ; ~Derivada() ;
virtual void put(const std::string& x) ; virtual virtual void put(const std::string& x, int z) ; virtual
std::string get() const ; virtual Base* clone() std::string get() const ;
const ; virtual int get_val() const ; virtual
protected: Derivada* clone() const ;
Base(const Base& o) ; protected:
private: Derivada(const Derivada& o) ;
std::string xx; private:
Base& operator=(const Base& o) ; int zz;
}; Derivada& operator=(const Derivada& o) ;
} };
#endif }
//--- base.cpp ----------------------- #endif
#include "base.hpp" //--- derivada.cpp -----------------------
using namespace std; #include "derivada.hpp"
namespace umalcc { using namespace std;
Base::~Base() {} namespace umalcc {
Base::Base() : xx() {} Derivada::~Derivada() {}
Base::Base(const string& x) : xx(x) {} Derivada::Derivada() : Base(), zz() {}
Base::Base(const Base& o) : xx(o.xx) {} Derivada::Derivada(const string& x, int z) : Base(x),
Base* Base::clone() const Derivada::Derivada(const Derivada& o) : Base(o), zz(o.
{ Derivada* Derivada::clone() const
return new Base(*this) ; {
} return new Derivada(*this) ;
void Base::put(const string& x) }
{ void Derivada::put(const string& x, int z)
xx = x ; {
} Base::put(x);
string Base::get() const zz = z ;
{ }
return xx ; string Derivada::get() const
} {
} return "XX" + Base::get() ;
//--- main.cpp ------------------------- }
#include <iostream> int get_val() const {
#include "modulo.hpp" return zz ;
using namespace std; }
using namespace umalcc; }
int main() //--- main.cpp -------------------------
{ #include <iostream>
Base* b = new Base; #include "modulo.hpp"
d->put("pepe"); using namespace std;
cout << d->get() << endl; using namespace umalcc;
delete b; int main()
} {
// g++ -ansi -Wall -Wextra -Werror -c base.cpp Base* b = new Derivada;
// g++ -ansi -Wall -Wextra -Werror -c main.cpp d->put("pepe");
// g++ -o main main.o base.o cout << d->get() << endl;
delete b;
}
// g++ -ansi -Wall -Wextra -Werror -c base.cpp
// g++ -ansi -Wall -Wextra -Werror -c derivada.cpp
// g++ -ansi -Wall -Wextra -Werror -c main.cpp
// g++ -o main main.o derivada.o base.o

4
g++ -ansi -Wall -Wextra -Werror -g -D GLIBCXX DEBUG -o main main.cpp modulos.cpp ... 2>&1|less
Contenedores STL: Vector Contenedores STL: Deque
#include <vector> #include <deque>
using namespace std; using namespace std;
typedef vector<int> VectorInt; typedef deque<int> DequeInt;
int main() int main()
{ {
VectorInt v1; DequeInt v1;
VectorInt v2(10); DequeInt v2(10);
VectorInt v3(10, 0); DequeInt v3(10, 0);
VectorInt v4 = v3; DequeInt v4 = v3;
if (v4 <= v3) { if (v4 <= v3) {
v4 = VectorInt(10); v4 = DequeInt(10);
v4.swap(v3); v4.swap(v3);
} }
for (int i = 0; i < v4.size(); ++i) { for (int i = 0; i < v4.size(); ++i) {
v4[i] = v4[i] * 2; v4[i] = v4[i] * 2;
} }
v4.clear(); v4.clear();
v4.push_back(3); v4.push_back(3);
v4.resize(10, 5); v4.resize(10, 5);
v4.resize(7); v4.resize(7);
if ( ! v4.empty() ) { if ( ! v4.empty() ) {
v4.pop_back(); v4.pop_back();
} }
} v4.push_front(7); // no en vector
v4.pop_front(); // no en vector
Contenedores STL: Vector 2D }

#include <vector> Contenedores STL: Stack


using namespace std; #include <stack>
typedef vector<int> FilaInt; using namespace std;
typedef vector<FilaInt> MatrizInt; typedef stack<int> StackInt;
int main() int main()
{ {
MatrizInt m1; StackInt s1;
MatrizInt m2(2, FilaInt(3)); StackInt s2 = s1;
MatrizInt m3(2, FilaInt(3, 0)); if ((s2 <= s1)||(s2.size() > 3)) {
MatrizInt m4 = m3; s2 = StackInt();
if (m4 <= m3) { }
m4 = MatrizInt(5, FilaInt(7)); s1.push(3);
m4.swap(m3); if ( ! s1.empty() ) {
} int cima = s1.top();
for (int f = 0; f < m4.size(); ++f) { s1.pop();
for (int c = 0; c < m4[f].size(); ++c) { m4[f][c] =
m4[f][c] * 2; }
}
}
}
Contenedores STL: Queue
m4.clear(); #include <queue>
for (int f = 0; f < 3; ++f) { using namespace std;
m4.push_back(FilaInt()); typedef queue<int> QueueInt;
for (int c = 0; c < 5; ++c) { int main()
m4[m4.size()-1].push_back(0); {
} QueueInt q1;
} QueueInt q2 = q1;
m4.resize(10, FilaInt(5, 2)); if ((q2 <= q1)||(q2.size() > 3)) {
m4.resize(7); q2 = QueueInt();
if ( ! m4.empty() ) { }
m4.pop_back(); q1.push(3);
} if ( ! q1.empty() ) {
} int primero = q1.front();
int ultimo = q1.back();
q1.pop();
}
}

5
g++ -Wall -o programa listac.cpp principal.cpp conjunto.cpp pila.cpp -std=c++11 2>&1 | less
Contenedores STL: Vector Contenedores STL: Deque
Contenedores STL: Deque
Cola::Cola():l(NULL){} void Cola::vaciar(TLista &l) const{
Cola::~Cola(){ TLista ult;
/*no seguro"Cola::"*/ vaciar(l); if(l!=NULL){
} ult=l;
Cola::Cola(const Cola &p){ l=ult->sig=NULL;
l=duplicar(p.l); ult->sig=NULL;
} while(l!=NULL){
Cola::Cola(const Cola &p){ ult=l;
l=duplicar(p.l); l=l->sig;
} delete ult;
Cola &Cola::operator=(const Cola &p){ }
if(this!=&p){ }
/*no seguro"Cola::"*/vaciar(l); }
l=duplicar(p.l);
} Contenedores STL: Stack
} class Pila{
void Cola::encolar(int v){ public:
TLista tmp,ult; Pila();
tmp=new TNodo; ~Pila();
tmp->dato=u; void apilar(const Conjunto &v);
tmp->sig=NULL; Conjunto desapilar();
if(l!=NULL){ bool vacia() const;
ult=l; Pila &operator=(const Pila &p);
while(ult->sig!=NULL){ Pila(const Pila &p);
ult=ult->sig; private:
} struct TNodoPila{
ult->sig=tmp; Conjunto conj;
}else{ TNodoPila *sig;
l=tmp; };
} typedef TNodoPila *TPila;
} TPila l;
int Cola::desencolar(){ TPila duplicar(TPila l);
int v; }
TLista tmp; Contenedores STL: Queue
if(l==NULL){ namespace tads{
std::cout<<"error"; class Cola{
exit(1); public:
} Cola();
v=l->dato; ~Cola();
tmp=l; Cola(const Cola &p);
l=l->sig; Cola &operator=(const Cola &p);
delete tmp; void encolar(int v);
return v; void desencolar();
} bool vacia() const;
Contenedores STL: Vector 2D private:
struct TNodo{
bool Cola::vacia() const{ int dato
return (l==NULL); TNodo* sig;
} };
Cola::TLista Cola::duplicar(TLista l) const{ typedef TNodo* TLista;
TLista rec,res,ult; TLista l;
res=NULL; TLista duplicar(TLista l) const;
ult=NULL; };
rec=l; }
while(rec!=NULL){ #endif
tmp=new TNodo; void Pila::apilar(int v){
tmp->conj=rec->conj; TLista tmp;
tmp->sig=NULL; tmp=new TNodo;
tmp->conj=v;
ult->sig=tmp;
tmp->sig=l;
ult=tmp; l=tmp;
rec=rec->sig; }
if(res!=NULL){ int Pila::desapilar(){
ult->sig=tmp; int v;
ult=tmp; if(l==NULL){
}else{ cout<<"error";
res=tmp; exit(1);
}
ult=tmp;
v=l->conj;
} TLista tmp;
rec=rec->sig; tmp=l;
} l=l->sig;
return res; delete tmp;
} return v;
}

También podría gustarte