Está en la página 1de 4

Ver: https://antares.sip.ucm.

es/cpareja/libroCPP/docs/4%20-
%20definici%C3%B3n%20de%20tipos%20-%20resumen.pdf

In [1]: int const N = 10;

In [3]: N = 18; //Justo para ilustrar que significa const

input_line_53:2:4: error: cannot assign to variable 'N' with const-qualifie


d type 'const int'

N = 18; //Justo para ilustrar que significa const

~ ^

input_line_42:2:12: note: variable 'N' declared const here

int const N = 10;

~~~~~~~~~~^~~~~~

In [4]: int v[N];

In [5]: cout << v <<endl; //intentar mandar un array de int a cout no es muy "produc

0x7f9bfc9d5020

In [6]: for(int i = 0;i<N;i++){

cout << v[i] << ",";

cout << endl;

0,0,0,0,0,0,0,0,0,0,

In [9]: void mostrar(int v[N]){

for(int i = 0;i<N;i++){

cout << v[i] << ",";

cout << endl;

In [10]: mostrar(v)

0,0,0,0,0,0,0,0,0,0,

String
In [26]: string s = "Hola mundo";

In [27]: s.length();

In [28]: s[5]

(char) 'm'

In [29]: s.append(". Adios, mundo cruel!");

In [30]: s

(std::string &) "Hola mundo. Adios, mundo cruel!"

In [31]: t = s + "\nAdios, mundo cruel."

(std::basic_string &) "Hola mundo. Adios, mundo cruel!

Adios, mundo cruel."

In [32]: cout << t;

Hola mundo. Adios, mundo cruel!

Adios, mundo cruel.

In [33]: int num_apariciones(string s,char c){

int result = 0;

for(int i = 0; i<=s.length();i++){

if(s[i]==c){

result++;

return result;

In [35]: cout << num_apariciones(t,'u')<< endl;

In [36]: char v[10] = "Hola"; //una forma más básica es usar un array de char, hereda

In [37]: cout << v;

Hola

In [38]: v.legth()

input_line_102:2:3: error: member reference base type 'char [10]' is not a


structure or union

v.legth()

~^~~~~~

In [41]: cout << strlen(v); //la longitud de v está determinada porque el final queda

In [42]: for(int i = 0;i< 10 ;i++){

cout<<v[i]<<endl;

} //fíjate como trata cout el caracter 0 ¡No escribe nada!

In [44]: for(int i = 0;i< 10 ;i++){

cout<<(int) v[i]<<endl;

} // los char no son otra cosa que un entero, que interpretamos como la letr

72

111

108

97

Ejercicio:
Escribir una función que devuelva las posiciones en que aparece el caracter c en la
cadena s

int[] apariciones(string s, char c){ ....... }

NO sirve: no podemos devolver un array

Una posibilidad es:

void apariciones(string s, char c, int ap[N], int & n_ap){ .......


} // Notar que el array ap no tiene &, los parámetros de tipo array
se pasan por "referencia". // Necesitamos el parámetro n_ap para
saber cuantas posiciones de ap estamos considerando

O,mejor aún, usar la técnica de https://antares.sip.ucm.es/cpareja/libroCPP/docs/4%20-


%20definici%C3%B3n%20de%20tipos%20-%20resumen.pdf
consistente en encerrar el
array dentro de un registro.

Archivos
In [11]: ifstream f;

f.open("texto.txt");

In [12]: f

(std::ifstream &) @0x7f6c5e5a7498

In [13]: string s;

getline(f,s);

In [14]: s

(std::string &) "Hola"

In [15]: getline(f,s);

cout << s;

Segunda linea

In [16]: getline(f,s);

cout << s;

Tercera y ultima sin número de linea

In [17]: getline(f,s);

cout << s;

Tercera y ultima sin número de linea

In [19]: getline(f,s);

(std::string &) "Tercera y ultima sin número de linea "

In [20]: string s1;

getline(f,s1);

s1

(std::string &) ""

In [21]: f.eof()

(bool) true

In [22]: f.close()

In [23]: f

(std::ifstream &) @0x7f6c5e5a7498

In [24]: f.is_open()

(bool) false

In [26]: f.open("texto.txt");

while(not f.eof()){

f >> s;
cout << s <<endl;

f.close();

Hola

Segunda

linea

Tercera

ultima

sin

salto

de

linea

linea

In [2]:

In [ ]:

También podría gustarte