[:tr]Programlarımızda, özellikle küçük olanlarda ya da bazı değerleri hızlıca görüntülemek için, en sık kullandığımız yöntemlerden birisi de standart çıktıya bir şeyler basmaktır.
Bunun için de kullanılan en yaygın yöntem, C++ için, std::cout‘tır muhtemelen. Peki bu kardeşimizin kabiliyetlerine ne kadar vakıfız? İşte bu yazımda bu kardeşimize ilişkin yetenekler ile sizleri buluşturacağım.
Öncelikli olarak standart çıktıya bastıklarınızı kontrol etmenize yarayan yapılara “I/O manipulator” denilmekte, biz bunlara Girdi/Çıktı Manipülatörleri diyeceğiz. Bunları kullanabilmek için “#include <iomanip>” başlık dosyasını kodunuzun içerisine eklememiz gerekiyor. Bu manipülatör’lerin çoğu aslında bir fonksiyon ve özel olarak <<, >> ile birlikte kullanmak üzere tasarlanmıştır, aynı zamanda fonksiyon gibi de çağırabilirsiniz (dikkatli okuyucularım, çoğu dediğimi fark etmişlerdir, çünkü setw, setprecision, setbase, setfill, resetiosflags, setiosflags bu kullanımın dışında). Hemen bir örneğe bakalım:
1 2 3 4 5 6 7 8 | bool value = false; std::boolalpha(std::cout); std::cout << value << std::endl; ile bool value = false; std::cout << std::boolalpha << value << std::endl; |
aynı çıktıyı verir.
Şimdi gelelim önemli manipülatörlere. Diğer yazılarımda olduğu gibi bana göre en önemli olanları, kalem kalem, sıralamaya çalışacağım. Tam liste için kaynaklar kısmındaki sayfalara başvurabilirsiniz.
- boolalpha, noboolalpha:
- Boolean değerlerin metinsel/nümerik gösterimi için kullanılır. Varsayılan nümerik (noboolalpha) gösterim aktiftir, true değerler 1 ve false değerler 0 olarak bastırılır. boolalpha ile birlikte bu değerler sırası ile “true ve “false” olarak bastırılır.
- showbase, noshowbase:
- showbase çağrıldığı durumda 8 düzene göre olan sayılar başların O, 16 lık düzene göre olanlar 0x olarak gösterilir.1234567891011#include <iostream>#include <iomanip>using namespace std;int main(){int n1 {10}; // 10 luk düzende 10int n2 {010}; // 8 lik düzene göre 8int n3 {0x10}; // 16 düzende 16cout << showbase << setbase(16) << "n1 = " << n1 << ", n2 = " << n2 << ", n3 = "<< n3 << "\n";return 0;}
- showbase çağrıldığı durumda 8 düzene göre olan sayılar başların O, 16 lık düzene göre olanlar 0x olarak gösterilir.
- showpoint, noshowpoint:
- showpoint kullanıldığı zaman, ondalık kısımlar her zaman gösterilir. Varsayılan olarak tam sayıya yuvarlanır.12auto n1 {10.0F};cout << "Once: " << n1 << showpoint << " Sonra:" << n1 << "\n";
- showpoint kullanıldığı zaman, ondalık kısımlar her zaman gösterilir. Varsayılan olarak tam sayıya yuvarlanır.
- showpos, noshowpos:
- showpos ile birlikte, pozitif sayıların önünde ek olarak ‘+‘ gösterilir. Varsayılan olarak gösterilmez.
- hexfloat, fixed scientific, defaultfloat:
- İsimlerinden de anlayacağınız üzere bu fonksiyonlar da, ondalık sayıların nasıl gösterileceğini belirliyor. Ayrıca, girdi olarak verilen değerleri de buna göre ayıklar. Bunları uzun uzadıya anlatmaktansa, sanırım örnek vermek daha iyi olacak.
- 12345678910111213#include <iostream>#include <sstream>using namespace std;int main(){cout << "0.01 - fixed: " << std::fixed << 0.01 << '\n'<< "0.01 - scientific: " << std::scientific << 0.01 << '\n'<< "0.01 - hexfloat: " << std::hexfloat << 0.01 << '\n'<< "0.01 - default: " << std::defaultfloat << 0.01 << '\n';double f;istringstream("0x1P-1022") >> std::hexfloat >> f;cout << "0x1P-1022 hex olarak ayikladigimizda elde ettigimiz deger:" << f << '\n';}
- setprecision(n):
- Ondalık bir sayı gösterildiği durumda, noktadan sonra gösterilecek rakam adetini belirler. Varsayılan adet 6’dır.12345678910111213141516#include <iostream>#include <iomanip>#include <cmath>#include <limits>using namespace std;int main(){const long double pi = std::acos(-1.L);cout << "default precision (6): " << pi << '\n'<< "setprecision(10): " << setprecision(10) << pi << '\n'<< "max precision: "<< setprecision(numeric_limits<long double>::digits10 + 1)<< pi << '\n';}
- Ondalık bir sayı gösterildiği durumda, noktadan sonra gösterilecek rakam adetini belirler. Varsayılan adet 6’dır.
- setbase(n):
- Girdi ve çıktı için kullanılan sayı sistemini belirler. 8, 10, 16 ve 0 alabilir. 0 çıktı için ondalık gösterim anlamına gelse de girdi için girilen sayıdan sayı sistemini belirle anlamına gelir.dec, hex, octTam sayılar için kullanılan sayı sistemini belirler. setbase(10), setbase(16) ve setbase(8) ile de aynı sonucu alabilirsiniz.
- ws:
- İlk boşluk olmayan karakteri görene kadar olan boşluk karakterlerini atar.123456789101112#include <iostream>#include <istream>#include <sstream>#include <string>int main(){std::istringstream s(" this is a test");std::string line;std::getline(s >> std::ws, line);std::cout << "ws + getline returns: \"" << line << "\"\n";}
- İlk boşluk olmayan karakteri görene kadar olan boşluk karakterlerini atar.
- ends:
- Çıktıya ‘\0’ ekler.
- flush:
- Çıktıya yığılan karakterleri bastırmak üzere gönderir.
- endl:
- Çıktıya ‘\n’ ekler ve bastırmak üzere gönderir.
- setiosflags(n)/resetiosflags(n):
- Verilen std::ios sabitlerini atar veya sıfırlar.12345678910#include <iostream>#include <iomanip>int main(){std::cout << std::resetiosflags(std::ios_base::dec)<< std::setiosflags( std::ios_base::hex| std::ios_base::uppercase| std::ios_base::showbase) << 42 << '\n';}
- Verilen std::ios sabitlerini atar veya sıfırlar.
- setw(n):
- Bir sonraki çıktı için tanımlanan genişliği belirler. Burada eğer bu alanda boşluk kalır ise setfill ile belirlenmiş olan karakter ile doldurulur.
- setfill(c):
- Boşlukları doldurmak için kullanılacak olan karakteri belirler. Buradaki boşluklardan kasıt, setw ile tanımlanan genişlikten daha az uzunlukta bir karakter gösterilmesinde durumunda kalan boşluklardır. Varsayılan olarak boşluk karakteri kullanılır.
- internal, left, right:
- Boşluk için belirlenmiş karakterlerin nereye yerleştirileceğini belirler. left ve right seçeneği her çıktı için kullanılabilecekken, internal sadece tam, ondalık sayı ve parasal gösterimler için kullanılır.
- 123456789101112131415161718#include <iostream>#include <iomanip>#include <locale>int main(){std::cout << "Left fill:\n" << std::left << std::setfill('*')<< std::setw(12) << -1.23 << '\n'<< std::setw(12) << std::hex << std::showbase << 42 << '\n';std::cout << "Internal fill:\n" << std::internal<< std::setw(12) << -1.23 << '\n'<< std::setw(12) << 42 << '\n';std::cout << "Right fill:\n" << std::right<< std::setw(12) << -1.23 << '\n'<< std::setw(12) << 42 << '\n';}
Evet arkadaşlar, artık uygulamalarınızda konsolu, standart çıktıyı, istediğiniz gibi kullanabilirsiniz 🙂
Bir sonraki yazımda buluşmak dileğiyle.
Kaynaklar:
- https://en.cppreference.com/w/cpp/io/manip
- http://www.cplusplus.com/reference/library/manipulators/
- https://www.tutorialspoint.com/What-are-Cplusplus-Manipulators-endl-setw-setprecision-setf
- https://www.uow.edu.au/~lukes/TEXTBOOK/notes-cpp/io/omanipulators.html
[:en]In our programs, especially for small ones, to display some values quickly, one of the most common methods we use is to print something on standard output.
For C ++, the most common method used for this is probably std :: cout. So how much are we familiar with this library’s abilities? In this article, I will bring you the associated features to your attention.
The structures that allow you to control what you print on the standard output are called “I/O manipulators”. To use them, you need to include the header file “#include <iomanip>” in your code. Most of these manipulators are actually a function and are specially designed to use with <<, >>, operators, but you can also call it as a function (as some of my readers have noticed, setw, setprecision, setbase, setfill, resetiosflags, setiosflags are not used this way). Let’s look at an example:
1 2 3 4 5 6 7 8 | bool value = false; std::boolalpha(std::cout); std::cout << value << std::endl; and bool value = false; std::cout << std::boolalpha << value << std::endl; |
give the same output.
Now, let us look at important manipulators. As in my other writings, I will try to focus on the most important ones, one by one. For a complete list, refer to the pages in the resources section.
- boolalpha, noboolalpha:
- Used for textual / numerical representation of boolean values. The default numeric (noboolalpha) notation is active, true values are displayed as 1 and false values are displayed as 0. With boolalpha, these values are displayed as “true” and “false“, respectively.
- showbase, noshowbase:
- In the case where showbase API is called, the numbers are displayed according to the 8 numbering base system are shown with 0, the ones according to the 16 basis are shown as 0x.1234567891011#include <iostream>#include <iomanip>using namespace std;int main(){int n1 {10}; // 10 luk düzende 10int n2 {010}; // 8 lik düzene göre 8int n3 {0x10}; // 16 düzende 16cout << showbase << setbase(16) << "n1 = " << n1 << ", n2 = " << n2 << ", n3 = "<< n3 << "\n";return 0;}
- In the case where showbase API is called, the numbers are displayed according to the 8 numbering base system are shown with 0, the ones according to the 16 basis are shown as 0x.
- showpoint, noshowpoint:
- When showpoint is used, decimal parts are always shown. By default, it is rounded to an integer.12auto n1 {10.0F};cout << "Once: " << n1 << showpoint << " Sonra:" << n1 << "\n";
- showpos, noshowpos:
- With showpos, positive numbers are displayed with “+”. Not displayed by default.
- hexfloat, fixed scientific, defaultfloat:
- As you can tell from their names, these functions determine how decimal numbers are displayed. It also extracts the values given as input accordingly. I think it would be better to give an example about their usage.
- 12345678910111213#include <iostream>#include <sstream>using namespace std;int main(){cout << "0.01 - fixed: " << std::fixed << 0.01 << '\n'<< "0.01 - scientific: " << std::scientific << 0.01 << '\n'<< "0.01 - hexfloat: " << std::hexfloat << 0.01 << '\n'<< "0.01 - default: " << std::defaultfloat << 0.01 << '\n';double f;istringstream("0x1P-1022") >> std::hexfloat >> f;cout << "0x1P-1022 hex olarak ayikladigimizda elde ettigimiz deger:" << f << '\n';}
- setprecision(n):
- When a decimal number is displayed, it determines the number of digits to be displayed after the period. The default is 6.
- 12345678910111213141516#include <iostream>#include <iomanip>#include <cmath>#include <limits>using namespace std;int main(){const long double pi = std::acos(-1.L);cout << "default precision (6): " << pi << '\n'<< "setprecision(10): " << setprecision(10) << pi << '\n'<< "max precision: "<< setprecision(numeric_limits<long double>::digits10 + 1)<< pi << '\n';}
- setbase(n):
- Specifies the numbering base system used for input and output which can be either 8, 10, 16 and 0. Although 0 is a decimal notation for output, it tells system to determine numbering base system automatically with respect to entered number for the input. You can get the same result with setbase (10), setbase (16) and setbase (8).
- ws:
- Removes the space characters until it sees the first non-space character.123456789101112#include <iostream>#include <istream>#include <sstream>#include <string>int main(){std::istringstream s(" this is a test");std::string line;std::getline(s >> std::ws, line);std::cout << "ws + getline returns: \"" << line << "\"\n";}
- Removes the space characters until it sees the first non-space character.
- ends:
- Adds ‘\ 0’ to the output.
- flush:
- Sends the output stacked characters to print.
- endl:
- Adds ‘\ n’ to the output and sends it to print.
- setiosflags(n)/resetiosflags(n):
- Assigns or resets the given std::ios values.12345678910#include <iostream>#include <iomanip>int main(){std::cout << std::resetiosflags(std::ios_base::dec)<< std::setiosflags( std::ios_base::hex| std::ios_base::uppercase| std::ios_base::showbase) << 42 << '\n';}
- Assigns or resets the given std::ios values.
- setw(n):
- Determines the width defined for the next output. If there is any space left in this field, it is filled with the character specified by setfill.
- setfill(c):
- Specifies the character to be used to fill in the blanks. The spaces herein refer to spaces that are displayed when a character less than the width defined by setw is displayed. The space character is used by default.
- internal, left, right:
- Specifies where to place the characters specified for the space. The left and right option can be used for each output, while internal is only used for integer, decimal, and monetary representations.123456789101112131415161718#include <iostream>#include <iomanip>#include <locale>int main(){std::cout << "Left fill:\n" << std::left << std::setfill('*')<< std::setw(12) << -1.23 << '\n'<< std::setw(12) << std::hex << std::showbase << 42 << '\n';std::cout << "Internal fill:\n" << std::internal<< std::setw(12) << -1.23 << '\n'<< std::setw(12) << 42 << '\n';std::cout << "Right fill:\n" << std::right<< std::setw(12) << -1.23 << '\n'<< std::setw(12) << 42 << '\n';}
- Specifies where to place the characters specified for the space. The left and right option can be used for each output, while internal is only used for integer, decimal, and monetary representations.
Yes my friends, now on you can use the console, standard output, in your applications as you like 🙂
Hope to meet you in my next article.
References:
- https://en.cppreference.com/w/cpp/io/manip
- http://www.cplusplus.com/reference/library/manipulators/
- https://www.tutorialspoint.com/What-are-Cplusplus-Manipulators-endl-setw-setprecision-setf
- https://www.uow.edu.au/~lukes/TEXTBOOK/notes-cpp/io/omanipulators.html
[:]
