|
You last visited: Today at 11:27
Advertisement
[C++] Ausgabe in Felder
Discussion on [C++] Ausgabe in Felder within the C/C++ forum part of the Coders Den category.
06/02/2014, 16:38
|
#1
|
elite*gold: 0
Join Date: May 2014
Posts: 18
Received Thanks: 0
|
[C++] Ausgabe in Felder
Wie reserviere ich eine gewisse anzahl an felder ?
Wenn ich folgendes mache
Code:
cout << setw(6) << setfill('*') << "ABC" << endl;
wird ausgegeben ich möchte aber dass folgendes ausgegeben wird
Dazu habe ich auch was kleines selbst geschrieben undzwar
Code:
cout << setw(9) << setfill('*') << "ABC";
cout << setw(6) << setfill('*') << "*";
Meine Frage ist nun was für möglichkeiten gibt es noch " ***ABC*** " ausgeben zu lassen außer
Code:
cout << "***ABC***" << endl;
Danke schon mal im vorraus.
|
|
|
06/02/2014, 20:21
|
#2
|
elite*gold: 312
Join Date: May 2013
Posts: 449
Received Thanks: 45
|
Quote:
Originally Posted by iTrodan
Wie reserviere ich eine gewisse anzahl an felder ?
Wenn ich folgendes mache
Code:
cout << setw(6) << setfill('*') << "ABC" << endl;
wird ausgegeben ich möchte aber dass folgendes ausgegeben wird
Dazu habe ich auch was kleines selbst geschrieben undzwar
Code:
cout << setw(9) << setfill('*') << "ABC";
cout << setw(6) << setfill('*') << "*";
Meine Frage ist nun was für möglichkeiten gibt es noch " ***ABC*** " ausgeben zu lassen außer
Code:
cout << "***ABC***" << endl;
Danke schon mal im vorraus.
|
Entweder mit printf oder mit sprintf_s
|
|
|
06/02/2014, 20:23
|
#3
|
elite*gold: 0
Join Date: Mar 2013
Posts: 4,917
Received Thanks: 1,379
|
|
|
|
06/02/2014, 21:17
|
#4
|
elite*gold: 724
Join Date: Mar 2011
Posts: 10,479
Received Thanks: 3,318
|
Quote:
Quote:
|
Entweder mit printf oder mit sprintf_s
|
Quote:
ôo
Dieser Code:
Code:
#include <iostream>
#include <string>
std::string foo(const std::string &text, char fill_char = '*', std::size_t rep = 3)
{
return std::string(rep, fill_char) + text + std::string(rep, fill_char);
}
int main() {
std::cout << foo("ABC") << std::endl;
std::cout << foo("ABC", '+', 5) << std::endl;
return 0;
}
gibt bei mir
Quote:
aus.
|
|
|
06/02/2014, 23:03
|
#5
|
elite*gold: 0
Join Date: Aug 2012
Posts: 236
Received Thanks: 94
|
Quote:
Originally Posted by snow911
|
Wenn man das noch etwas "verbessert" und sich alle Mühe gibt, das Ergebnis "wiederverwendbar" und die Zeilen lang zu halten, kann man sich sogar seinen eigenen Manipulator erstellen:
Code:
#include <iostream>
#include <string>
#include <utility>
#include <cstddef> // std::size_t
template <typename char_type, typename traits = std::char_traits <char_type>>
struct special_default_initialization_values {
static constexpr char_type default_fill_char = 0;
static constexpr std::size_t default_count = 3;
};
template <typename traits>
struct special_default_initialization_values <char, traits> {
static constexpr char default_fill_char = '*';
static constexpr std::size_t default_count = 3;
};
template <typename traits>
struct special_default_initialization_values <wchar_t, traits> {
static constexpr wchar_t default_fill_char = L'*';
static constexpr std::size_t default_count = 3;
};
template <typename string_type>
struct special {
using special_string = string_type;
using special_ostream = std::basic_ostream <typename string_type::value_type, typename string_type::traits_type>;
using default_initialization_values = special_default_initialization_values <typename string_type::value_type, typename string_type::traits_type>;
static constexpr typename string_type::value_type default_fill_char = default_initialization_values::default_fill_char;
static constexpr std::size_t default_count = default_initialization_values::default_count;
special_string text;
special_string filler;
explicit special (special_string text, typename string_type::value_type fill_char = default_fill_char, std::size_t count = default_count)
: text {std::move (text)}, filler {count, fill_char} {}
friend special_ostream & operator << (special_ostream & ostream, const special & s) {
return ostream << s.filler << s.text << s.filler;
}
};
template <typename string_type, typename char_type = typename string_type::value_type, typename traits = typename string_type::traits_type>
special <string_type> format (string_type text, char_type fill_char = special <string_type>::default_fill_char, std::size_t count = special <string_type>::default_count) {
return {std::move (text), fill_char, count};
}
template <typename char_type, typename traits = std::char_traits <char_type>>
special <std::basic_string <char_type, traits>> format (const char_type * text, char_type fill_char = special <std::basic_string <char_type, traits>>::default_fill_char, std::size_t count = special <std::basic_string <char_type, traits>>::default_count) {
return special <std::basic_string <char_type, traits>> {text, fill_char, count};
}
int main () {
std::cout << format ("text") << std::endl;
std::cin.get ();
}
Ideones std::basic_string-Konstruktor hat einen Fehler, es wird immer nur ein Zeichen produziert.
Die C++14-Variablentemplates wären hilfreich gewesen.
|
|
|
 |
Similar Threads
|
pixel felder vergleichen
03/21/2014 - AutoIt - 4 Replies
heyho,
ich wollte mal fragen ob es eine funktion in autoit gibt mit der man ein ganzes feld an pixeln vergleichen kann.
So nach dem motto, lies alle pixel zwischen x,y,x2,y2 aus und gucke ob du sie in dem bereich daneben findest.
einfach ein pixelsearch für ganze bereiche statt einzelnen pixeln.
(sinn dess ganzen ist es, ein bild zu erkennen und zu schauen ob es sich nochmal auf der website irgendwo befindet, und falls dass der fall ist soll es dann gelöscht werden ^^ aber den rest...
|
Wv Felder hat die Accounttabelle?
02/06/2011 - Metin2 Private Server - 0 Replies
Hallo,
könnte mir jemand sagen, wieviele Felder die Accounttabelle hat?
Danke
Jaffa
PS: Wenn jemand für ne Gegenleisung (30e*g) mir auchnoch alle Namen raussucht, soll derjenige sich per PN melden.
|
DT FELDER
06/13/2010 - Metin2 - 0 Replies
Kennt jemand ne Seite wo alles aufgezeichnet ist auf welchem Feld der jeweilige Schmied kommt? Weil cih hab gar keine Ahnung
|
Wofür sin dir felder?
03/21/2010 - Last Chaos - 13 Replies
http://i40.tinypic.com/2d7tzew.png
^^
Ich frage deswegen
http://i41.tinypic.com/6q9l54.png
|
Koordinaten Felder Snakes
02/28/2010 - Metin2 Private Server - 0 Replies
Ich brauche die Koordinaten Felder Schlangen?
Vielen Dank im Voraus.
|
All times are GMT +1. The time now is 11:28.
|
|