Posts

Showing posts with the label Stroustrup

C++ 11's new style, explained by Bjarne Stroustrup

Spanish version / Versión en Español In a series of very interesting presentations in Microsoft's GoingNative2012 event, C++'s main creator, Bjarne Stroustrup himself, talked about the programming style introduced by the brand new C++11 standard. The main focus of Bjarne's presentation was not the new language features , but how to use them correctly; that's what he calls "style". Up next, a summary of the key points raised: Type-rich interfaces: Programmer-defined suffixes can now be used to indicate measuring units (see the new constexpr keyword). Use RAII to handle resources: memory, files, sockets, hardware, operating system resources (threads, mutexes, semaphores, etc). Avoid raw pointer usage, specially in public interfaces (restrict those usages to single method implementation scope). Use smart pointers classes such as unique_ptr and shared_ptr . Don't use the latter if the resource needs to be shared. And before con...

C++ 11 y su nuevo estilo, explicado por Bjarne Stroustrup

Versión en inglés / English version En una serie de charlas muy interesantes del evento GoingNative2012 organizado por Microsoft, el mísmisimo creador de C++, Bjarne Stroustrup, dio una charla sobre el nuevo estilo de programación introducido por el nuevo estándar C++ 11. El tema de la charla no son las nuevas funcionalidades del lenguaje , sino cómo usarlas correctamente; eso es lo que Bjarne denomina "estilo". A continuación, un resumen de los puntos esenciales: Type-rich interfaces: Se puede usar sufijos para indicar unidades, por ejemplo (ver la nueva keyword constexpr ). Usar RAII para recursos: memoria, archivos, sockets, hardware, recursos del sistema operativo (threads, mutexes, semáforos, etc). Evitar el uso de punteros, especialmente en interfaces (mantener esos usos acotados a implementaciones de funciones). Usar smart pointers como unique_ptr y shared_ptr . No usar este último si no se está compartiendo el recurso. Y antes de consid...

C++: Parse input skipping whitespace

Spanish version / Versión en Español Still re-reading Stroustrup's book , I found something I'd like to have known some time ago: an elegant way to parse an input stream for words separated by whitespace (1 whitespace = any amount of consecutive space and/or tabs). This can be done using an istream_iterator, like so: #include <fstream> #include <iostream> #include <algorithm> #include <vector> void main(){ using std::cout; using std::endl; // We read from standard input, but it could just as well be a file std::istream_iterator<std::string> ii( std::cin ); cout << "--->Input whitespace-separated words:" << endl; // Suppose we stop reading when we find the word "END" std::vector<string> words; while ( *ii != "END" ){ cout << *ii << endl; words.push_back(*ii); ii++; } // Sort words cout << "Sorted words:...

C++: Parsear entrada separando palabras

Versión en inglés / English version Releyendo el libro de Stroustrup , encontré algo que me habría gustado saber tiempo atrás: una forma piola de parsear un stream de entrada separando palabras con whitespace ( 1 whitespace = cualquier cantidad consecutiva de espacios y/o tabs ). Esto se hace con un istream_iterator, así: #include <fstream> #include <iostream> #include <algorithm> #include <vector> void main(){ using std::cout; using std::endl; // Leemos desde standard input, pero podria ser desde un archivo std::istream_iterator<std::string> ii( std::cin ); cout << "--->Entrada parseada separando por whitespace:" << endl; // Supongamos que dejamos de leer cuando leemos el string "FIN" std::vector<string> palabras; while ( *ii != "FIN" ){ cout << *ii << endl; palabras.push_back(*ii); ii++; } // Ordenamos las palabras cout ...