El comienzo no ha estado mal, pero me he atascado un poquito:
Este es mi código completo:
Código
//"Toying around with strings and vars" #include <iostream> #include <string> using namespace std; int main() { //declaring strings and vars string str1, str2, str3; char testchar1; int count, pos; //setting initial strings str1 = "This is a test String"; str2 = "One"; //playing with size instructions cout << "Size: " << str1.size() << endl; cout << "Capacity: " << str1.capacity() << " (Before reserve)" << endl; str1.reserve(64); str2.reserve(64); cout << "Capacity: " << str1.capacity() << " (After reserve)" << endl; //Toying with 'at' instruction cout << "Chars per position:" << endl; cout << "--" << endl; for (size_t count=0; count < str1.length(); count++) { cout << "[" << count << "] -> " << str1.at(count) << endl; } cout << "\n"; //Testing append instruction cout << "str1 contains: " << str1 << endl; cout << "str2 contains: " << str2 << endl; str2.append(4,'-'); str2.append("Two"); cout << "Now, str2: " << str2 << endl; //Swapping the content of str1 and str2 str1.swap (str2); cout << "After a swap: \n"; cout << "str1 contains: " << str1 << endl; cout << "str2 contains: " << str2 << endl; //Experimenting with find and substr pos = str1.find("String"); str3 = str1.substr (pos); cout << "I found the word: " << str3 << endl; cout << "It starts at position: " << pos << endl; //Trying out string comparison: //int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) if (str3.compare(0,6,str2,14,6)) cout << "Found " << str3 << " into " << str1 << endl; if (str1.compare(0,3,str3,0,6)) cout << "Weird... I found " << str2 << " into " << str3 << "!\n"; else cout << "Didn't find " << str2 << " into " << str3 << endl; return 0; }
La ejecución ha ido bien hasta un punto cercano al final, aquí muestro el retorno en consola:
Código:
Size: 21
Capacity: 21 (Before reserve)
Capacity: 64 (After reserve)
Chars per position:
--
[0] -> T
[1] -> h
[2] -> i
[3] -> s
[4] ->
[5] -> i
[6] -> s
[7] ->
[8] -> a
[9] ->
[10] -> t
[11] -> e
[12] -> s
[13] -> t
[14] ->
[15] -> S
[16] -> t
[17] -> r
[18] -> i
[19] -> n
[20] -> g
str1 contains: This is a test String
str2 contains: One
Now, str2: One----Two
After a swap:
str1 contains: One----Two
str2 contains: This is a test String
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Viendo dónde se ha quedado clavado, el problema parece estar en alguna parte del código después del comentario:
Código:
//Experimenting with find and substr
Mi compilador (Dev-C++) ya no me da errores, así que no se me ocurre qué puedo haber hecho mal...
¿Me echáis una mano? Lo estoy pasando muy bien trasteando, y tengo curiosidad por saber en qué fallé, y si hay alguna manera algo mejor de usar alguna de las instrucciones que me han funcionado aquí.
Un Saludo, y miauchísimas gracias por adelantado. :3